in reply to writing to a file
Open a handle to the old file. Open a handle to a new file. Print your start material to the new file. Run through the old file in chunks and print it to the new file. Delete (unlink) the old file. Rename the new file to the old file name.