LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-01-2013, 11:29 AM   #1
pranavojha
Member
 
Registered: Feb 2006
Location: Bangalore, India
Distribution: Fedora Core 3
Posts: 68

Rep: Reputation: 15
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 ?

Last edited by pranavojha; 02-01-2013 at 11:30 AM. Reason: edit
 
Old 02-01-2013, 11:56 AM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by pranavojha View Post
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.

Last edited by johnsfine; 02-01-2013 at 11:59 AM.
 
  


Reply

Tags
file, pointer


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
two words in a file Aerosilver Programming 2 11-12-2012 11:05 PM
extract words from a xml file vicky007aggrwal Linux - Newbie 5 08-18-2012 02:53 PM
Holding file pointer of deleted file postiwala Linux - Software 1 02-16-2012 11:11 AM
[SOLVED] Adding file extensions to listings in a txt file mrjawz Linux - Newbie 5 02-15-2012 08:53 AM
Getting file path from FILE pointer in C barisdemiray Programming 3 10-22-2004 12:58 AM


All times are GMT -5. The time now is 03:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration