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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With