#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c=0;
ifstream fin("test.txt",ios::in);
if(!fin){
cout<<"error"<<endl;
return
}
while(!fin.eof()){
fin.get(c);
cout<<c;
}
system("pause");
return 0;
}
如果text.txt的内容是:123456,那么输出的结果是1234566;
如果数据:123456后面还有一个空行,就不会显示重复(其实重复了一个空行,只是我们看不到而已)
后来通过Google,发现eof()返回true的条件是“读到文件结束符”,而不是文件内容的最后一个字符。
要清楚“文件结束符”(0xff),就是说我们文件内容最后的字符不是文件结束符,而最后的字符的下一位才是。
所以操作再读多一次,才到达文件结束符的位置。
#include <fstream>
#include<iostream>
using namespace std;
int main()
{
char c = 0;
ifstream file("test.txt");
if