#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream fp;
char ch;
fp.open("/home/rithik/C++/OOP/a.txt",ios::in);
if(!fp){
cout<<"FILE can't be opened \n";return 0;
}
else
cout<<"File opened \n";
while(!fp.eof()) {// return true if eof reached i.e. end of file
fp>>ch; // file copied character by character
cout<<ch;
}
cout<<endl;
fp.close();
return 0;
}
为什么会这样,所以文件a.txt中的最后一个字符被打印两次 例如a.txt是“你好” 运行此代码后,我得到结果
Helloo