简体   繁体   English

如何检查 stringstream>>string 是否不会在字符串上放置任何内容?

[英]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.请注意,第 3 行有一个空字符串(没有或空格)。问题是,当您执行 stringstream>>laststring 并且 stringstream 有一个空字符串(空或只是空格)时,它不会覆盖“laststring” ”,它什么都不做。 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.我不想创建一个临时空字符串只是为了检查它在 stringstream>> 之后仍然是空的,看起来很蹩脚。

When you cannot read from stream - its state changes, so when casting to bool, it returns false:当您无法从流中读取时 - 它的状态会发生变化,因此当转换为 bool 时,它会返回 false:

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

Or - in if-expr :或者 - 在if-expr

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

See example查看示例

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, ie, in your case above you need to split the lines first or use something like std::getline() to read the second part of line.但是请注意,从流中读取不是基于行的,即在上面的情况下,您需要先拆分行或使用std::getline()类的东西来读取行的第二部分。

You can use getline, to read a line from the file.您可以使用 getline 从文件中读取一行。 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 .您需要包括iostreamfstreamsstreamstring

For checking if string is empty, use foo.size() == 0 .要检查字符串是否为空,请使用foo.size() == 0

For checking if string stream is empty fooStream.rdbuf()->in_avail() == 0用于检查字符串流是否为空fooStream.rdbuf()->in_avail() == 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM