Q:

overwrite a text in a file using c++

I have a problem with the following code :
     fstream s("tst.txt" , fstream::binary);
     s.seekp(5, fstream::beg);
     s.write("testing", 7);

it should keep the first five character in the file as it is,but it doesn't !
it discard the whole old data,i have to keep the old data and overwrite just a block of file data , and it should be in a binary mode 
any one help?
thanks in advance.

A:

Try this:
fstream s("tst.txt" , fstream::binary | fstream::in | fstream::out);
s.seekp(5, std::fstream::beg);
s.write("testing", 7);

By specifying an openmode flag,you are overriding the default openmode in your fstream constructor call.

