【发布时间】:2016-02-14 18:26:06
【问题描述】:
根据您的建议,我已按照您的建议更改了代码,但是当将 ios::out 替换为 ios::ate 时仍然存在问题,文件中没有写入任何内容(写入不起作用)。有没有办法检查下一位是否是 eof 而不是读取它然后检查它?正如你所建议的那样。有时当我进行文件处理时,它会显示文件指针的位置为-1,这意味着什么???
代码:
int main ()
{
char p[80];
fstream file("text1.txt",ios::out|ios::in); //if ios::ate is added here it results into infinite loop
cout<<"Starting position of the file is "<<file.tellg()<<endl;
getch();
if(file.is_open())
cout<<"file is open\n";
else
cout<<"file is not open\n";
getch();
file.seekp(0);
while(file>>p)
{
cout<<p<<endl;
}
file.clear();
cout<<"\nThe current position of the file pointer is "<<file.tellg()<<endl;
file.seekp(0);
if(file.eof())
cout<<"\n the eof\n";
while(file>>p)
{
cout<<p<<endl;
}
file.close();
return 0;
}
输出:
Starting position of the file is 0
file is open
Hello
man
how
are
you
The current position of the file pointer is 21
Hello
man
how
are
you
【问题讨论】: