C ++覆盖特定位置文件中的数据 [英] C++ overwriting data in a file at a particular position
问题描述
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屋!