0

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! :)

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by Barca
0

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

Edited by Ancient Dragon: n/a

0

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

0

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';
}

Edited by Ancient Dragon: n/a

0

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!

Edited by Barca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.