c++ - In what practical case bool(std::ifstream) != std::ifstream::good()? -
i know in case can have :
bool(std::ifstream) != std::ifstream::good()
the difference bool(std::ifstream)
not test eof
bit whereas std::ifstream::good()
tests it. practically, eof
bit raised if 1 try read after end of file. try think either fail
or bad
bit set.
consequently in case can raise eof
bit ?
simply put, whenever encounter end of file without attempting read behind it. consider file "one.txt" contains 1 single '1' character.
example unformatted input:
#include <iostream> #include <fstream> int main() { using namespace std; char chars[255] = {0}; ifstream f("one.txt"); f.getline(chars, 250, 'x'); cout << f.good() << " != " << bool(f) << endl; return 0; }
0 != 1
press key continue . . .
example formatted input:
#include <iostream> #include <fstream> int main() { using namespace std; ifstream f("one.txt"); int i; f >> i; cout << f.good() << " != " << bool(f) << endl; return 0; }
0 != 1
press key continue . . .
Comments
Post a Comment