How to clear stringstream? [duplicate]

Typically to ‘reset’ a stringstream you need to both reset the underlying sequence to an empty string with str and to clear any fail and eof flags with clear.

parser.str( std::string() );
parser.clear();

Typically what happens is that the first >> reaches the end of the string and sets the eof bit, although it successfully parses the first short. Operations on the stream after this immediately fail because the stream’s eof bit is still set.

Leave a Comment