rdstate()返回值意味着什么? [英] what does rdstate() return value means?

查看:363
本文介绍了rdstate()返回值意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

istream& Read(istream &is)
{
    std::string buf;
    while (is >> buf)       
    {   
        cout << is.eofbit << " " << is.failbit << " " << is.badbit << endl;
        cout << is.rdstate() << endl;
        cout << buf << endl;
    }
    cout << is.eofbit << " " << is.failbit << " " << is.badbit << endl;
    cout << is.rdstate() << endl;
    is.clear();
    cout << is.eofbit << " " << is.failbit << " " << is.badbit << endl;
    cout << is.rdstate() << endl;
    return is;
}

如果我输入普通字符如test,则输出为 1 2 4 0

然后我输入CTRL + Z(窗口),输出 1 2 4 3 1 2 4 0

If I input normal characters like "test",the output is 1 2 4 0.
Then I type CTRL+Z (windows),the output is 1 2 4 3 1 2 4 0.

问题:
1. rdstate()返回值是什么意思? (为什么输出3而不是2?不是1?)

Question : 1. what does rdstate() return value means? (Why does it output 3,not 2? not 1?)


  1. 为什么没有我键入CTRL + Z后is.eofbit is.failbit 更改? (正如 C ++ Primer 5th Editon 所说,达到文件结尾设置eofbit和failbit)

  1. Why did not is.eofbitand is.failbit change after I typed CTRL+Z ? (As C++ Primer 5th Editon says,Reaching end-of-file sets both eofbit and failbit )


推荐答案

成员 std :: ios :: rdstate()只返回状态标志的组合 std: :ios_base :: badbit std :: ios_base :: eofbit ,以及 std :: ios_base :: failbit 。在哪些条件下设置的条件不完全一致但意图如下:

The member std::ios::rdstate() simply returns a combination of the state flags std::ios_base::badbit, std::ios_base::eofbit, and std::ios_base::failbit. Under which conditions which bits gets set isn't entirely consistent but the intent is the following:


  1. std :: ios_base :: badbit 在流处于真正功能失调的状态时被设置,你[可能]不会从中获取任何东西。例如,如果没有流缓冲区或流上的任何操作引发异常,则设置此标志。

  2. std :: ios_base ::当输入操作失败时,failbit 被设置,例如,因为格式化的输入操作有意外的字符。可以通过清除它,忽略几个字符并再次尝试来从此错误中恢复。

  3. std :: ios_base :: eofbit 在达到[当前] EOF时设置,即当前不再提取任何字符时。

  1. std::ios_base::badbit gets set when the stream is in genuinely dysfunctional state and you [probably] won't get anything out of it. For example, this flag is set if there is no stream buffer or when any of the operations on the stream has thrown an exception.
  2. std::ios_base::failbit gets set when an input operation failed, e.g., because a formatted input operation got an unexpected characters. It may be possible to recover from this error by clearing it, ignoring a few characters, and trying again.
  3. std::ios_base::eofbit gets set when [the current] EOF is reached, i.e., when there could be no more characters extracted for now.

现在,在您的情况下,您输入了一个字符串并且读取它是成功的,即没有设置标志。请注意,使用换行符停止读取,即您确实输入了test \ n,并且流提取了这五个字符。然后,当您结束流时,流在尝试读取字符串时达到EOF,即设置 std :: ios_base :: eofbit 输入失败还设置 std :: ios_base :: failbit

Now, in your case you entered a string and reading it was successful, i.e., there are no flags set. Note that reading stopped with the newline character, i.e., you really entered "test\n" and the stream extracted these five characters. When you then ended the stream, the stream reached EOF while trying to read a string, i.e., it set std::ios_base::eofbit and the input failed also setting std::ios_base::failbit.

如果你只想看到 std :: ios_base :: eofbit 设置,你可以使用一个流,该流以流的末尾的一个单词结尾,没有任何后续的空格字符。获得这样一个流的简单方法是使用 std :: istringstream 并从中读取:

If you want to see only std::ios_base::eofbit set, you can do so by using a stream which ends with a word right at the end of the stream without any following space character. An easy way to get such a stream is to use an std::istringstream and read from that:

std::istringstream in("test");
Read(in);

另一个简单的设置是看 std :: ios_base :: badbit set:你只需创建一个没有流缓冲区的流,例如:

Another easy set up is to see std::ios_base::badbit set: you'd just create a stream without a stream buffer, e.g.:

std::istream in(0);
Read(in);

请注意,流最初将具有 std :: ios_base :: badbit 设置并获取 std :: ios_base :: failbit 在尝试读取字符时设置。在 clear()之后,仍然会设置 std :: ios_base :: badbit

Note that the stream will initially have std::ios_base::badbit set and also get std::ios_base::failbit set upon an attempt to read a character. After clear()ing the std::ios_base::badbit will still be set, though.

要获得 std :: ios_base :: failbit 设置而不必拥有 std :: ios_base :: eofbit 设置你需要阻止它看到一个非空白字符:默认情况下, std :: string 的输入操作符开始跳过空格然后读取,直到它达到空格或EOF,如果它可以读取至少一个非空白字符则成功。这样做的方法是关闭自动跳过空白,例如:

To get std::ios_base::failbit set without also having std::ios_base::eofbit set you'll need to prevent it from seeing a non-whitespace character: the input operator for std::string by default start off skipping whitespace and then reads until it either reaches whitespace or EOF and it is successful if it could read at least one non-whitespace character. An approach to do that is to turn automatic skipping of whitespace off, e.g.:

std::istringstream in("test test");
Read(in >> std::noskipws);

BTW,请注意,不保证 std ::的值ios_base :: eofbit std :: ios_base :: failbit ,或 std :: ios_base :: badbit 除了可以用作某种形式的位掩码之外。

BTW, note that there is no guarantee for the values of std::ios_base::eofbit, std::ios_base::failbit, or std::ios_base::badbit other than they can be used as bitmasks in some form.

这篇关于rdstate()返回值意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆