Shai Balassiano
Shai Balassiano

Reputation: 1007

how to "push" a word (string) to a given position in a file without overwriting the text (programming c)

I wonder how can I update an existing file, and add a word in a given position.

so let say my file looks like:

this the first line in the file

and I want to add the word "is " in position 6 so the file will look like:

this is the first line in the file

what is the best method to achieve that? what should be the fopen mode? assume my file is to big to copy to memory, or create a temporary clone

thanks!

Upvotes: 2

Views: 109

Answers (2)

i0exception
i0exception

Reputation: 392

Unfortunately, it is not possible to simply update a file this way. If it is a flat file, you will have to move the parts yourself.

Upvotes: 0

Mat
Mat

Reputation: 206709

There is no magic "insert in the middle" open mode. You have to do that yourself.

If it can't fit in memory, and you don't want/can't create a temporary, you can rewrite it "from the bottom". (I.e. read the last "block", write it back shifted by the amount you want, repeat.)

Upvotes: 2

Related Questions