Hello! I am studying for an exam in my programming I class, and I'm having some trouble with one of the example problems. The problem is to write a C++ function that reads from an input file, starting at a certain line (passed into function and called "int startingLine"). I'm very unsure of how to read the file in starting at "int startingLine" and was wondering if anybody had any advice.
I'm very unsure of how to read the file in starting at "int startingLine" and was wondering if anybody had any advice.
Simply read and discard the first startingLine - 1 lines from the file, then begin processing. This should be as simple as calling std::getline in a loop.
To start processing at the third line:
1 2 3 4 5 6 7
int starting_line = 3;
for (std::string line; starting_line > 1 && stream; --starting_line)
std::getline(stream, line);
if (! stream) { /* handle a stream error */ }
// process the rest of the file
To make things easier, you might write a function to drop the first n lines from the file.