Hiya.
I'm using std::getline to read lines from a file, how can I move forward a few lines? do I have to use getline the number of lines I want to skip?
thanks!
Hiya.
I'm using std::getline to read lines from a file, how can I move forward a few lines? do I have to use getline the number of lines I want to skip?
thanks!
Yes use std::getline
unless you know the location of the newlines.
If for some strange reason you happen to know the location of where the newlines appear then you can use ifstream::seekg
first.
You can read in other ways such as ifstream::read
but std::getline
is probably the easiest and most clear solution.
do I have to use getline the number of lines I want to skip?
No, but it's probably going to be the clearest solution to those reading your code. If the number of lines you're skipping is large, you can improve performance by reading large blocks and counting newlines in each block, stopping and repositioning the file to the last newline's location. But unless you are having performance problems, I'd just put getline in a loop for the number of lines you want to skip.
For what it's worth:
void skip_lines(std::istream& pStream, size_t pLines)
{
std::string s;
for (; pLines; --pLines)
std::getline(pStream, s);
}