You might also want to take a look at
seek,
tell, and
truncate. You can lock the file, open it once with
+>> (read/write access in append mode, so
you don't blow away whatever's already in there), then
use these calls to do everything you need to do before
closing and unlocking the file.