C ++覆盖特定位置文件中的数据 [英] C++ overwriting data in a file at a particular position

查看:156
本文介绍了C ++覆盖特定位置文件中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题覆盖一些在c + +中的文件中的数据。我使用的代码是

pre $ code int main(){
fstream fout;
fout.open(hello.txt,fstream :: binary | fstream :: out | fstream :: app);
pos = fout.tellp();
fout.seekp(pos + 5);
fout.write(####,4);
fout.close();
返回0;

}

使用seekp,数据总是写在最后。我想写在一个特定的位置。
如果我不添加fstream ::应用程序,文件的内容被删除。
Thanks。

解决方案

问题在于 fstream :: app - 它打开要附加的文件,这意味着所有写入文件的末尾。为了避免内容被删除,请尝试打开 fstream :: in ,这意味着打开 fstream :: binary | fstream :: out | fstream :: in


i m having problem in overwriting some data in a file in c++. the code i m using is

 int main(){
   fstream fout;
   fout.open("hello.txt",fstream::binary | fstream::out | fstream::app);
   pos=fout.tellp();
   fout.seekp(pos+5);
   fout.write("####",4);
   fout.close();
   return 0;

}

the problem is even after using seekp ,the data is always written at the end.I want to write it at a particular position. And if i dont add fstream::app , the contents of the file are erased. Thanks.

解决方案

The problem is with the fstream::app - it opens the file for appending, meaning all writes go to the end of the file. To avoid having the content erased, try opening with fstream::in as well, meaning open with fstream::binary | fstream::out | fstream::in.

这篇关于C ++覆盖特定位置文件中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆