I am trying to write some simple code which will read a text file but reads the first line twice. I thought this would be as simple as something like this
std::ifstream file;
file.open("filename", std::ios_base::in);
std::string line;
std::getline(file, line);
// process line
file.seekg(0, ios::beg);
while (std::getline(file, line))
{
// process line
}
However the seekg must fail as the first line is not processed twice. Any idea why?
PLEASE NOTE: This is not the problem I am faced with but a simplified version of it so as not to have to paste multiple classes code and multiple functions. The real problem involves a file pointer being passed to multiple functions in multiple classes. The first function may or may not be called and reads the first line of the file. The second function reads the whole file but must first call seekg to ensure we are at the beginning of the file.
I just used the code above to simplify the discussion.
Rather than seeking back to the beginning and reading the first line twice, I think I'd approach things with something like:
At the moment, this assumes that
process
doesn't modifyline
(but if needed, it's easy enough to make an extra copy of it for the first call).Edit: given the modified requirements in the edited answer, it sounds like the seek is really needed. That being the case, it's probably cleanest to
clear
the stream before proceeding:Ideally your code should look like this: