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 eofbit is allowed to be set after an open: badbit may be set if the actual open throws an exception (I think—the standard doesn't really say what should happen in this case); failbit should be set if the open fails, or if the seek after the open (if ate is set) fails; but there doesn't seem to be any case where eofbit may 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, calling std::istream::good() is probably not the right solution.)
If std::ifstream::good() returns false, the next input will fail.
If it returns true, it tells you nothing: the next input may succeed,
but it might also fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With