无法存储stdin状态并在C ++中进行比较

时间:2015-03-31 16:19:12

标签: c++ eof

using namespace std;

int main(void)
{
    int input;

    while (true)
    {
        cin >> input;
        ios_base::iostate state = cin.rdstate();

        if (state == ios_base::eofbit)
    //  if (state == (ios_base::eofbit | ios_base::failbit)) doesn't work also
            break;
    }

    return EXIT_SUCCESS;

}

为什么在按下^D(EOF,Windows中为^Z)时它永远不会停止?

1 个答案:

答案 0 :(得分:2)

无法读取格式化的输入集failbit以及eofbit。要测试eof,请检查该位:

if (state & ios_base::eofbit) // if you like verbosity
if (cin.eof())                // if you like brevity