LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Adding words to a file using file pointer in C (https://www.linuxquestions.org/questions/programming-9/adding-words-to-a-file-using-file-pointer-in-c-4175448193/)

pranavojha 02-01-2013 11:29 AM

Adding words to a file using file pointer in C
 
Hi,
I am using file pointers to add some content to an XML file. The problem is that writing contents seem to be overwriting the other text. For example, my file has the following content:
Code:

Hello every, this is C.
I want to insert body in the text so the content looks like this:
Code:

Hello every body, this is C.
For this, I fseek my pointer beyond 'y' in 'every,' and add ' body', which ends up writing the file content like this:
Code:

Hello every, body is C.
This actually overwrites word 'this'. How can I do this using file pointer without overwriting file content?

I can think of writing content to a diff file, deleting original file and renaming that file later to original file to accomplish this.

Is there any other way of inserting word in file ?

johnsfine 02-01-2013 11:56 AM

Quote:

Originally Posted by pranavojha (Post 4882408)
I can think of writing content to a diff file, deleting original file and renaming that file later to original file to accomplish this.

Good.

Quote:

Is there any other way of inserting word in file ?
You could do a rolling buffered read/rewrite of the entire remainder of the original file, avoiding copying the part before the insert.

But that may be less safe and it is more complicated.


All times are GMT -5. The time now is 07:51 PM.