One way is to 'slurp' the file into an array (1 rec per element), then amend array, then re-write file.
There's a good example here (http://www.goldb.org/goldblog/CommentView.aspx?guid=a858e606-de5d-4378-b538-8d4738cd9438)
if I'm allowed to point to it ...
Cheers
Chris