in reply to Re: Append to text file not working
in thread Append to text file not working

I meant to mention locking. If you have multiple users (i.e. threads, processes, pieces of code) adding to the file simultaneously, you'll need to control access to the file. See flock.

Replies are listed 'Best First'.
Re^3: Append to text file not working
by tilly (Archbishop) on Feb 21, 2008 at 23:21 UTC
    As long as your writes are below one page of data (2k is safe), then with most filesystems your writes are atomic and no locking synchronization is needed. That looks like it is true in this case.

    If locking synchronization is needed, then flock is not necessarily the right place to start looking because (depending on the filesystem) it can be highly unreliable.

      I'm sure that you meant to specify that your writes are atomic, when writing to files that were opened for append.

      I prefer that posts in public forums err towards the pedantic side. :)

      -Colin.

        If you want to get pedantic, the actual writes to the filesystem (in memory at least) are atomic in either case. However if you have not set append (or, reading some C documentation, if you're unlucky enough to be using NTFS) it is the case that one process could write to a location in the file that another process has already overwritten, thereby overwriting what the other process wrote.

        So technically the writes are atomic, but the end result could still disappoint. :-)