Edit a file

Hi everyone,

I want to know how can I edit a file in the middle of it without erasing its content.

For example, if I have a file with the text:

"I love apples"

and i'd want to edit it to:

"I really love apples".

If I do something like this:

1
2
3
4
5
6
file.seekp(2);

file<<"really ";

etc..


the previous text is erased:

"I really ples"

How do I do it?

Concurrently reading and writing to a file is trickier that it might seem. Generally you would read the file, edit it in memory, and then overwrite the original. Is there some specific reason you want to modify a file this way?
Actually no, I am just starting to learn the basics of I/O and I wondered about all sorts of basic techniques. And since it is a very simple thing to do in a text editing software...

Anyway, I suppose the most basic way of doing it is having an input stream get all the characters and copy them to another file untill the editing point is reached, make the change, and then continue getting input from the previous file to fill the rest.
Topic archived. No new replies allowed.