From: David Masover Date: 2010-05-20T08:25:26+09:00 Subject: Re: Write to the top of a file On Wednesday, May 19, 2010 05:25:13 pm Ian Hobson wrote: > On 19/05/2010 22:28, John Wu wrote: > > Hi, > > > > I want to write some content to an existing file and I want to new > > content be the first line of the file. How to do that? [snip] > For file that will fit in RAM, load it, join with your first line, and > save it all. > > For larger files, you need to copy and rename. Or for files whose contents you actually care about -- and many of those are small files. Think about what happens if you crash. The typical method is to create a temporary file, insert the data, append the original file, then rename on top of the original file. Then the file is never in a corrupted state, even temporarily.