Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 289
Icebone1000 Avatar asked Oct 16 '12 22:10

Icebone1000


1 Answers

When you cannot read from stream - its state changes, so when casting to bool, it returns false:

bool read = static_cast<bool>(ss >> laststring);

Or - in if-expr:

if (ss >> laststring) 
    cout << "Just read: " << laststring;

See example

like image 58
PiotrNycz Avatar answered Oct 24 '22 22:10

PiotrNycz