I want to read from two files until I reach the end of one of them. If something goes wrong, the fstream should throw an exception.
The problem is, that bad or fail bit is also set when eof bit is set.
ifstream input1;
input1.exceptions(ios_base::failbit | ios_base::badbit);
input1.open("input1", ios_base::binary | ios_base::in);
ifstream input2;
input2.exceptions(ios_base::failbit | ios_base::badbit);
input2.open("input2", ios_base::binary | ios_base::in);
ofstream output;
output.exceptions(ios_base::failbit | ios_base:: badbit);
output.open("output", ios_base::binary | ios_base::out | ios_base::trunc);
char in1, in2, out;
while(!input1.eof() && !input2.eof()) {
input1.read((char*) &in1, 1);
input2.read((char*) &in2, 1);
out = in1^in2;
output.write((const char*) &out, 1);
}
input1.close();
input2.close();
output.close();
This leads to
$ ./test
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear
How to do it right?