file.close()失灵?
看看下面的代码
void main()
{
fstream file1("test.txt",ios::out|ios::binary),file2;
char a('a');
if(file1.fail())
{
cout<<"wrong1"<<endl;
exit(1);
}
file1.write(&a,sizeof(a));
file1.close();
file2.open("test.txt",ios::in|ios::binary);
if(file2.fail())
{
cout<<"wrong2"<<endl;
exit(1);
}
while(!file2.eof())
{
file2.read(&a,sizeof(a));
cout<<a<<endl;
}
file2.close();
}
明明只写了一个字节a进去 文件大小查看了一下也是1个字节
但是第一次读完按说file2.eof() 应该返回false了
但又读了一次。。。。。这怎么搞的?该怎么解决啊?