In example below, file exceptions are set. Inside the try-catch block is validating the size of the read data using the gcout()
function. Is it necessary to test the return value of gcount()
when the exceptions are set?
#include <iostream>
#include <fstream>
int main()
{
char buf[100];
std::ifstream file;
file.exceptions(std::ios_base::badbit | std::ios_base::failbit | std::ios_base::eofbit);
try {
file.open("file.bin", std::ios_base::in | std::ios_base::binary);
file.read(buf, sizeof buf);
// Is this error check redundant?
if (file.gcount() != sizeof buf) {
std::cout << "gcount(): Read error\n";
}
file.close();
} catch (...) {
std::cout << "Exception: Read error\n";
}
return 0;
}