By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

Editing Files: Help!

P: n/a
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).

How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?

Jul 31 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rik
On Tue, 31 Jul 2007 03:10:10 +0200, ttrium <ar*****@gmail.comwrote:
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).

How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?
You cannot 'prepend' data. You'll have to fetch the content, write your
new content to the beginning of the file, and then add the old content.
--
Rik Wasmus
Jul 31 '07 #2

P: n/a
On Jul 30, 8:15 pm, Rik <luiheidsgoe...@hotmail.comwrote:
On Tue, 31 Jul 2007 03:10:10 +0200, ttrium <arco...@gmail.comwrote:
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).
How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?

You cannot 'prepend' data. You'll have to fetch the content, write your
new content to the beginning of the file, and then add the old content.
--
Rik Wasmus
Is there a simpler way by using databases?

Jul 31 '07 #3

P: n/a
ttrium wrote:
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).

How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?
Two ways:

1. Open the file, read it all into memory, truncate the file, write the
new data followed by the old data.

2. Open the file, open a temporary file, write the new data to the
temporary file, copy the data from the original file to the temporary
file, close both files. delete the original file and rename the
temporary file.

#2 is obviously more work, but it doesn't require lots of memory to read
a large file into memory, and won't lose the file if the system crashes
in the middle.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.