7

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.

Icebone1000
  • 1,231
  • 4
  • 13
  • 25

4 Answers4

23

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

PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
2

You can only know after trying to read whether there was something or not. What you might be able to do is to skip whitespace and see if there is a non-space in the next location:

if ((in >> std::ws).peek() != std::char_traits<char>::eof()) {
    ...
}

Given that empty strings are cheap to create, I wouldn't bother and try read the string. Note, however, that reading from streams isn't line based, i.e., in your case above you need to split the lines first or use something like std::getline() to read the second part of line.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • From the wording of the question, I think he already has a `getline`, and is reading from the stringstream. otherwise, reading would not "have an empty string" and would go to the next line. – Mooing Duck Oct 16 '12 at 22:11
  • @MooingDuck: Well, since he is worried about creating an empty string, it seem be odd to first read into a line ;-) That said, I read the question as reading a single line. Otherwise skipping whitespace wouldn't work, either. – Dietmar Kühl Oct 16 '12 at 22:17
0

You can use getline, to read a line from the file. Then, copy the line into a string stream and read words from the string stream one at a time. The streams will automatically stop reading when they run out of lines / words.

// open file
std::ifstream fin("text.txt");

// 'iterate' through all the lines in the file
unsigned lineCount = 1;
std::string line;
while (std::getline(fin, line))
{
    // print the line number for debugging
    std::cout << "Line " << lineCount << '\n';

    // copy line into another stream
    std::stringstream lineStream(line);

    // 'iterate' through all the words in the line
    unsigned wordCount = 1;
    std::string word;
    while (lineStream >> word)
    {
        // print the words for debugging
        std::cout << '\t' << wordCount++ << ' ' << word << '\n';
    }
}

You need to include iostream, fstream, sstream and string.

Ryan
  • 2,378
  • 1
  • 19
  • 29
0

For checking if string is empty, use foo.size() == 0.

For checking if string stream is empty fooStream.rdbuf()->in_avail() == 0

Armado
  • 5
  • 1
  • 6
  • 3
    `std::string` has an [`empty()`](https://en.cppreference.com/w/cpp/string/basic_string/empty) member function that will directly tell you if it's empty without having to compare its size to some value. – Blastfurnace Nov 20 '20 at 17:45