I have a simple C++ file read program:
int main() {
std::string buf;
std::ifstream file;
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
file.open("C:\\Test11.txt");
char c;
while (!(file.eof())) {
file.get(c);
std::cout << c;
}
}
catch (std::ifstream::failure e) {
std::cout << e.what() << std::endl;
std::cout << e.code() << std::endl;
std::cout << "Exception opening/reading file";
}
file.close();
return 0;
}
The content of file at C:\Test11.txt
is Hello
.
The program is able to read the content but fails with exception ios_base::failbit
. It seems to have problem while evaluating while (!(file.eof()))
.
What is going wrong?