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
)时它永远不会停止?
答案 0 :(得分:2)
无法读取格式化的输入集failbit
以及eofbit
。要测试eof
,请检查该位:
if (state & ios_base::eofbit) // if you like verbosity
if (cin.eof()) // if you like brevity