Search code examples
file-iofile-accessfile-sharing

Is there any reason to open a file with shared-write access?


I've always opened my files two ways -- either read access and shared-read, or read/write access and no sharing.

To me it seems that allowing shared-write could always result in unexpected things happening to the file while you're reading it. Are there any good reasons to open a file in shared-write mode?


Solution

  • If a file is shared by many processes, it is sometimes impractical to lock the whole file (for performance reasons).

    In this case, you can lock a region of the file while it is being written.

    In Windows you might use the function LockFile().
    In Linux/Unix you might use fcntl() or flock()