Pietro
Pietro

Reputation: 13172

fstream: cannot write in the middle of a file

I cannot write text in the middle of a file. I can correctly find the point where to add the text, and I can check it with tellg()/tellp(). But when, after a seekp(), I add the new text:

myfstream << "new text" << endl;

This is appended to the end of the file.

What is wrong in what I am doing?

Upvotes: 2

Views: 389

Answers (2)

Pietro
Pietro

Reputation: 13172

As reported here, since I opened the file in append mode, then every write operation will append, even after a seekp().

The solution is to open the file in ios::in | ios::out mode.

Upvotes: 1

Lord_Curdin
Lord_Curdin

Reputation: 954

Sorry there's no way to write in the middle of a file with fstream. You must copy the hole file into you programm and edit your text and write it back into the file.

Otherwise you could overwrite the line with std::ostream::seekp()

Upvotes: 0

Related Questions