求助fstream类中eof()函数的问题
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
fstream file1;
file1.open("Ex1.txt",ios::in);
if(!file1)
{
cout<<"Can't open Ex1.txt!\n";
return;
}
fstream file2;
file2.open("Ex2.txt",ios::out|ios::trunc);
if(!file2)
{
cout<<"Can't create Ex2.txt!\n";
file1.close();
return;
}
char ch;
while(!file1.eof())
{
file1.read(&ch,1);
cout<<ch;
file2.write(&ch,1);
}
file2.close();
file1.close();
getchar();
}
以上是完整代码,我在目录下创建了一个Ex1.txt文件,内容为"what",可是显示在屏幕上却是"whatt",写到Ex2.txt中也是这样,我想知道为什么最后一个字符会重复一次呢,有什么解决办法吗?