Check following simple C++
program:
#include <iostream>
using namespace std;
int
main()
{
char ch;
while (cin >> ch)
{
cout << ch << '\n';
}
cout << "bad: " << cin.bad() << ", eof: " << cin.eof() << ", fail: " << cin.fail() << '\n';
return 0;
}
Compile and run it, press Ctrl + D
:
$ c++ foo.cpp -o foo
$ ./foo
bad: 0, eof: 1, fail: 1
We can see both fail
and eof
bits are set to 1
. From this table, we can see when both fail
and eof
bits are set to 1
, the operator bool
of stream will return false
.