Use eof() to read and display a text file.
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
char ch;
ifstream fin("text.txt");
if(!fin) {
cout << "Cannot open file.\n";
return 1;
}
do {
fin.get(ch);
if(!fin.eof() && (fin.fail() || fin.bad())) {
cout << "Input Error\n";
fin.close();
return 1;
}
if(!fin.eof()) cout << ch;
} while(!fin.eof());
fin.clear();
fin.close();
if(!fin.good()) {
cout << "Error closing file.";
return 1;
}
return 0;
}
Related examples in the same category