gpt4 book ai didi

c++ - 无法存储标准输入状态并在 C++ 中进行比较

转载 作者:搜寻专家 更新时间:2023-10-31 02:23:22 28 4
gpt4 key购买 nike

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)时它永远不会停止?

最佳答案

读取格式化输入失败设置 failbit 以及 eofbit。要测试 eof,只需检查那个位:

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

关于c++ - 无法存储标准输入状态并在 C++ 中进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29373474/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com