Error flags are set on a stream object whenever some operation on it fails. Once the stream is in error, no further operations can succeed on it until you reset the error state.
Here's a simple example:
std::istringstream iss("ABC123");
int n;
iss >> n;
assert(!iss);
Now the stream is in error. However, there's still data in it, and it might be worthwhile resetting and trying again:
iss.clear(); // note: this must come first!
std::string token;
iss >> token;
assert(iss);
Instead of another extraction, you could also call iss.ignore()
after the clear()
, in case you know what you have to ignore before it makes sense to try again.
Usually, this sort of trial-and-error isn't a very good approach, I find. I would always use getline
on the stream first to get complete lines. This can only fail when the stream has been exhausted (end of file, or end of string). Then you can proceed to process each line by a dedicated parsing logic, and errors can be handled in the most appropriate way. The error flags on the original stream are too crude to allow for elegant control flow design.