Ignore heading line if input file

I am trying to read a file with the following format

Ticker Date Open High Low Close Up Vol Dn Vol SPX
~NYSE 3/1/1965 550 561 550 561 27.5 22.4 87.25
~NYSE 3/2/1965 590 590 520 520 34.8 17.1 87.4

When I read the first line I get an error and it will not process the rest of the file. The code is below. I have tried many ways to ignore the line and step to the first data line but I can't figure out how.

This code uses peek and if the ~ in the ticker name isn't read I try to read the next line. But if fails and does not read the file from that point on.

What must I do to ignore the first line in a file if it is not the same format as the data?

If I remove the header line from the in file the program works fine.

Files are opened before this point.

for (int i=0; ;i++)
float o, h, l, c, uv, dv, spx;
char pdate[12], ticker[12];
char x = infile.peek();
if(x == '~')
infile >> ticker >> pdate >> o >> h >> l >> c >> uv >> dv >> spx;
outfile << ticker << '\t' << pdate << '\t' << o << '\t' << h << '\t' << l << '\t' << c << '\t' << uv << '\t' << dv << '\t' << spx << '\n';
return 0;
The problem is that when the next character in the file is not a ~ at the beginning of the loop, you do nothing. So when it loops, the next character in the file is the same character it was previously, ad nauseum.

The smart thing to do would be to just ignore the first line entirely, then enter the loop so you don't need to process that special case for the first line for every line of the file.

Before the loop:

infile.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;

And of course you'd need to #include <limits> . If you're not comfortable with that yet, any number that's going to be larger than the length of a line you'd read in will do for the first argument to infile.ignore.

Then you can remove the if/else and continue bit in the loop. Maybe you can figure out what you should be looping on, too!
Every line in the file, except the heading, starts with ~, part of the ticker name and is constant. If that varied I would have to change the code.

I am using peek for ~ to detect eof. The infile.eof never finds the end of the file, which really seems stupid. So I coded it to look at the first char in the line. When it reads after the eof the first char in the line is not ~ and I exit. I really don't understand why eof doesn't work on a formatted file. The code above has continue but that was changed to break to exit the loop.

I will try the ignore to see if it ignores the first line in the file. Else, it is easier to just delete the header line before I process the file. I already did that to continue testing.

It's pretty rare you actually want to check for eof. Normally you should just loop until an input operation fails. Then, you know you're done.

    // ...

    infile.ignore( std::numeric_limits<std::streamsize>::max(), '\n') ;
    float o, h, l, c, uv, dv, spx;
    char pdate[12], ticker[12];

    while ( infile >> ticker >> pdate >> 0 >> h >> l >> c >> uv >> dv >> spx )
        outfile << ticker << '\t' << pdate << '\t' << o << '\t' << h << '\t'
            << l << '\t' << c << '\t' << uv << '\t' << dv << '\t' << spx << '\n' ;
    // ... 
Thanks that works great. From the function definition I could not tell that the >> operator returned anything. Seems to return BOOL.
Topic archived. No new replies allowed.