Hi, I know this is asking for much and I'm being lazy but I have an ASCII text file with multiple rows of text. each row contains the name of the city followed by a ">" and then the countries name.

I wanted to know if its possible to have a C++ program that removes the anything written before the ">" in each line.

any help would be great!
thank you! :)

Recommended Answers

All 5 Replies

Yes of course its possible. All you have to do is rewrite the entire file

open input file for read
open temp output file for writing
for read line read from input file
   remove what you want from the line
   write the rest to the output file

close both files
delete input file
rename output file to the name of the input file
end of program

ok im still working on this and ive made some progress, but how do i tell the program to delete everything in the line up until and including characters: "/>

sorry for the nooby questions

I would read a line, tokenize it with the > as delimiter and drop the part I want and write to another file. Finally as AD have said remove old and rename current

assuming you are using c++ (afterall, this is the c++ forum) Put this in your program and run it to see what it does.

#include <string>
#include <iostream>
int main()
{
   std::string line = "abcdeft>hijklmn";
   // locate the > character
   size_t pos = line.find('>');
   // if found ...
   if( pos != std::string::npos)
       line = line.substr(0,pos);
   st::cout << line << '\n';
}

assuming you are using c++ (afterall, this is the c++ forum) Put this in your program and run it to see what it does.

#include <string>
#include <iostream>
int main()
{
   std::string line = "abcdeft>hijklmn";
   // locate the > character
   size_t pos = line.find('>');
   // if found ...
   if( pos != std::string::npos)
       line = line.substr(0,pos);
   st::cout << line << '\n';
}

Yess, this is exactly it... Except I wanted the first part removed so i just changed

line = line.substr(0,pos);

to

line = line.substr(pos+1);

1 more thing plz :) .
how do i use the if command to search through the line and delete the line if it doesnt contain atleast 1 of 2 characters?
for example:

Line 13
Line 2
Line 3
Line 123

if the line does not contain either of characters "1" and "3", delete

output:
Line 1
Line 3
Line 1232

i really appreciate your help !! thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.