Reputation: 35
I'm wondering what is the best way to read what the next string is in a file (to check if it matches a string) without increasing the position of the cursor in the file. So basically I want to read the next string without reading the next string.
Upvotes: 1
Views: 1406
Reputation: 1811
You cannot read the next string without changing the cursor as long as you use the same ifstream object, but you can save and restore the position:
auto p = stream.tellg();
// do what ever read you like
stream.seekg(p, std::ios_base::beg);
If your read failes you may have to clear the error flags before calling seekg().
Upvotes: 2