In C++, is there a case where std::ifstream open() can be successful, but std::ifstream good() can be false ?
EDIT : tested with g++ 4.7.1
#include <iostream>
#include <fstream>
int main(int argc, char *argv[])
{
std::ifstream filestream("testfile");
std::cout<<filestream.good()<<std::endl;
std::cout<<filestream.eof()<<std::endl;
std::cout<<filestream.fail()<<std::endl;
std::cout<<filestream.bad()<<std::endl;
return 0;
}
will return : 1, 0, 0, 0 for an empty file which means good = TRUE and eof = fail = bad = FALSE. Is it normal ?
After verifying the actual text in the standard, I don’t think
eofbitis allowed to be set after anopen:badbitmay be set if the actual open throws an exception (I think—the standard doesn’t really say what should happen in this case);failbitshould be set if the open fails, or if the seek after the open (ifateis set) fails; but there doesn’t seem to be any case whereeofbitmay be set.Not that calling
std::istream::good()is a good solution in this case. (It would be interesting to know what the OP is trying to achieve. Whatever it is, callingstd::istream::good()is probably not the right solution.)If
std::ifstream::good()returnsfalse, the next input will fail.If it returns
true, it tells you nothing: the next input may succeed,but it might also fail.