gpt4 book ai didi

C++ 文件处理:ios::ate 无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 21:17:05 26 4
gpt4 key购买 nike

在下面提到的代码中,当我应用 ios::out 时它可以正常工作,但在 ios::ate 的情况下却不是这样,它显示文件中的某个位置 -1 并且无法写入文件。

其次peek()函数用在什么地方

代码:

int main ()

{
char p[80];
fstream file("text1.txt",ios::in|ios::ate);
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();
if(file<<"Now if we add text to the file")//Not working
cout<<"\n Data entry possible\n";
else
cout<<"\n Data entry not possible\n";
file.flush();
cout<<"\nThe current position of the file pointer is "<<file.tellg()<<endl;//Showing position -1
file.clear();
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 30
file is open
Now
if
we
add
text
to
the
file

Data entry not possible

The current position of the file pointer is -1
Now
if
we
add
text
to
the
file

最佳答案

ate 并不意味着 out 所以如果你想写入文件你仍然需要使用 out

fstream file("text1.txt",ios::in|ios::out|ios::ate);

关于C++ 文件处理:ios::ate 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33719035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com