user2764874
user2764874

Reputation: 3

how to overwrite a particular data in file using c++

I am facing problem while overwriting a file at a particular position in C++. I have opened my file using ios::app|ios::binary. Then I seek to that position which I want to overwrite. But instead of overwriting it, I append from the end of the file. Any suggestions?

Upvotes: 0

Views: 265

Answers (2)

Diego Giagio
Diego Giagio

Reputation: 1057

You can open your file with ios::in|ios::out|ios::binary. After that you should use seekp() to seek to the desired position of the file before the write.

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129314

You need to specify ios::in as well as ios::out. It overwrites if you have only ios::out (default in ofstream). As stated ios::app means data is written to the end of the file, which is not what you want.

Upvotes: 9

Related Questions