C/C++ ifstream之eof()
2016-05-11 09:49
429 查看
参考来自:http://blog.csdn.net/rebel_321/article/details/4927464
程序实测可用:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str;
ifstream fin("D:\\1.txt"/*, ios::binary*/);
if (fin.peek() == EOF)
{
cout << "file is empty." << endl;
return 0;
}
while (!fin.eof())
{
fin >> str;
cout << str<<' ' ;
}
system("pause");
return 0;
}
程序实测可用:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str;
ifstream fin("D:\\1.txt"/*, ios::binary*/);
if (fin.peek() == EOF)
{
cout << "file is empty." << endl;
return 0;
}
while (!fin.eof())
{
fin >> str;
cout << str<<' ' ;
}
system("pause");
return 0;
}