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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -