JDoe4444
JDoe4444

Reputation: 35

Quick way to read the next string from a file without moving the position of the file

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

Answers (1)

Andreas H.
Andreas H.

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

Related Questions