Skipping header lines using istream

I am working an an assignment where I use istream to read a text file of weather data into a struct array. My problem is that throughout the file there are header lines that I need to ignore.

I wanted to use the getline function and set up an if statement to check and see if it was a header line and then ignore it. However, my problem is that if it is NOT a header line, using getline turns that line into a string and I can't use the extraction operator to separate the data into separate variables.

I wish there was some sort of putback feature for an entire line.

Any tips?

Here is an example of the the time of data/header lines I'm working with:

2017 Temp. (¯F) Precip. (in) Events
Jan high avg low sum
1 39 36 32 0.04 "Rain , Snow"

And here is the section of code I'm struggling with:
[code]
struct Weather {
int month;
int date;
int high;
int avg;
int low;
double precip;
string event;
};

Weather days[200];
int currMonth = 1;
char peekIt;


while (!file.eof()){


//check and see if it's a header (only the header line has the number 1 in the 2nd spot (2017))

//file.seekg(2);
peekIt = file.peek();
if (peekIt == '1'){
//ignore next two lines
file.ignore(256, '\n');
file.ignore(256, '\n');
currentMonth++;
}

else {
//look for next number and start inputing data
file.seekg(0);
file >> days[i].date >> days[i].high >> days[i].avg >> days[i].low >> days[i].precip;
getline(file, days[i].event);


//establish month
days[i].month = currMonth;
}
Why not just attempt to read a line of data? If that puts the input stream into a fail state, clear it and ignore the line. If it doesn't, you've extracted a line of data.
Topic archived. No new replies allowed.