PDA

View Full Version : using fwrite to append text to an existing file?


noizFACTORY
07-02-2008, 09:51 AM
Hi,

When I use fwrite command, it overwrites all the data in my file with the new string. But I don't want that. Instead I just want to add that string to my file withtout deleting its old content. is that possible?

Thanks!

-Sachin

digitalandrew
07-02-2008, 09:57 AM
Hi

When you fopen your file for writing, use the "a" flag for appending, as in...

$fileId = ` fopen $filename "a"`;

noizFACTORY
07-02-2008, 06:31 PM
Hi

When you fopen your file for writing, use the "a" flag for appending, as in...

$fileId = ` fopen $filename "a"`;

Thank you Andrew for your response. The append flag will actually add the text at the end of existing content of the file. What if I want to add ti anywhere within the file? I should have been clearer though. What I want to do is:

say, I have a file dummy.txt which already contains some text. Now, I want to add a line, "this is the new line" after the 3rd line and again after the 7th line. Is it possible to do that? I hope I'm clear enough.

Norb
07-02-2008, 09:29 PM
what i usually do is read all the lines into a "source" array, and then you can step through each line testing against a given value and write the lines to a "target" array, then all you have to do is write the "target" to the file, stomping over whatever is there.

CGTalk Moderation
07-02-2008, 09:29 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.