how to get end of the line

hi...

i hav a ASCII file in which, at the beginning there is a header lins.
and then there r numerical lines, then again header llines with numerical lines...sequentially it follows.

i want to read these lines & header lines..
how can i loop on unknow no. of lines per header lines.

do you mean that you want to read every line of a file
Read next line and try to read its content as a number. If you succeed then the line is numerical (or at least it starts with a number):
1
2
3
4
5
6
7
8
char line[1000];
while(...)
{
  gets(line);
  int n;
  if( sscanf(line, "%d", &n) == 1 )
     //then the line starts with a number
}


Or you may try to introduce a convension about header lines, for instance, all of them begin with '#'. Then you have only to analyze the first symbol of the next read line.
A more cleaner approach would be to begin each header line with a special token.
Then the code looks like follows (I use # to mark a header line):

1
2
3
4
5
6
7
8
9
std::string line;
std::ifstream ifs("data.txt");
while (std::getline(ifs, line))
{
    if (line[0] == '#')
    { /* handle header line */ }
    else
    { /* handle numerical line */ }
}
@melkiy: gets() and sscanf() are both unsafe, not to mention there's no reason to use C style strings when C++ strings are available.
Last edited on
have a look at
boost::regex

have you some example data?
Topic archived. No new replies allowed.