#include <iostream>
#include <fstream>
#define LOG(x) std::cout << #x << " = " << x << std::endl
int main() {
{
std::cout << "ofstream(\"/tmp\")" << std::endl;
std::ofstream f("/tmp");
LOG(f.good());
LOG(f.eof());
LOG(f.fail());
LOG(f.bad());
LOG(f.rdstate());
}
std::cout << std::endl;
{
std::cout << "ifstream(\"/tmp/\")" << std::endl;
std::ifstream f("/tmp");
LOG(f.good());
LOG(f.eof());
LOG(f.fail());
LOG(f.bad());
LOG(f.rdstate());
}
std::cout << std::endl;
{
std::cout << "ifstream(\"/tmp/notexistingfile\")" << std::endl;
std::ifstream f("/tmp/notexistingfile");
LOG(f.good());
LOG(f.eof());
LOG(f.fail());
LOG(f.bad());
LOG(f.rdstate());
}
}
Output:
ofstream("/tmp")
f.good() = 0
f.eof() = 0
f.fail() = 1
f.bad() = 0
f.rdstate() = 4
ifstream("/tmp/")
f.good() = 1
f.eof() = 0
f.fail() = 0
f.bad() = 0
f.rdstate() = 0
ifstream("/tmp/notexistingfile")
f.good() = 0
f.eof() = 0
f.fail() = 1
f.bad() = 0
f.rdstate() = 4