您的位置:首页 > 其它

Use eof() to read and display a text file.

2012-11-28 14:45 459 查看
#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;
}/**
while(!fin.eof())
{
char bufline[256];
fin.getline(bufline, 100);
}
*/



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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: