How to check if stringstream>>string will put nothing on the string?

Icebone1000 picture Icebone1000 · Oct 17, 2012 · Viewed 16.2k times · Source

For example, when parsing a text file, some times this file have stuff like this:

keyword a string here
keyword another string
keyword 
keyword again a string

Note that the 3th line have an empty string (nothing or white spaces).. The thing is that when you do stringstream>>laststring, and stringstream have an empty string (null or just white space), it will not overwrite the "laststring", it will do nothing. Theres anyway to check this situation before hand? I dont want to create a temp empty string just to check it is still empty after stringstream>>, seems lame.

Answer

PiotrNycz picture PiotrNycz · Oct 17, 2012

When you cannot read from stream - its state changes so when casting to bool will return false:

bool read = (ss >> laststring);

See ideone example