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.

4 Answers 4

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

3
  • ninja! this is it, Im just waiting the 5 min to accept the answer. Oct 16, 2012 at 22:14
  • 1
    This doesn't compile for me on modern g++. Weirdly, if I copy and paste the exact same code into ideone it doesn't even work there, despite ideone claiming it's using the same compiler. Jun 23, 2021 at 14:05
  • 1
    @DanielMcLaury you're right. Now - this operator bool is explicit. Answer edited.
    – PiotrNycz
    Jun 23, 2021 at 17:57
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.

2
  • 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. Oct 16, 2012 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. Oct 16, 2012 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.

1
  • pretty much what Im doing, what I didnt know is the checking of the operation as bool. Oct 16, 2012 at 22:31
0

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

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

1
  • 3
    std::string has an empty() member function that will directly tell you if it's empty without having to compare its size to some value. Nov 20, 2020 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.