Home > Mobile >  Ignore commemts while parsing txt file C
Ignore commemts while parsing txt file C

Time:05-19

I have a large text file and I am parsing using string stream. Text file looks like this

#####
##bjhbv
nvf
vbhjbj
vfjbvjf
*bj
*bvjbv
.
.
.
.
 FILE
data I want to parse from here to 
.
.
.
.
-FILE 
till here
#shv again comments
.
.

How can I parse only between FILE to -FILE? I can parse inside off it, but i just want to ignore above and below comments. Please help me how to ignore whie reading or parsing from .txt file. Any leads will be appreciated.,

CodePudding user response:

Something like this should do it:

{
  ifstream f_in(input_file_name);
  ofstream f_out(output_file_name);

  string line;
  // discard up to " FILE"
  while (getline(f_in, line) && line != " FILE");
  // copy up to "-FILE"
  while (getline(f_in, line) && line != "-FILE") f_out << line << endl;
}

The ifstream and ofstream will close their respective files upon leaving scope, which is why I showed them within {}.

CodePudding user response:

As you see in the comments, you can discard above FILE. you can use std::istream::ignore

You can give delimiter as FILE then you can process your logic. And put a break with a condition once the '-FILE` match.

  • Related