Search code examples
c++windowsfile-locking

Read a file while another program locked it


I have a program which reads a file written by a software. But the software locks the file while writing to it (I cannot even copy the file!). How can I read the file while it is writing to it?

I'm using Windows 7 with NTFS filesystem, my program is written in C++.


Solution

  • The program which writes to the file should open it with read sharing enabled, otherwise other processes can't read from the file that is not closed yet:

    FILE_SHARE_READ

    Enables subsequent open operations on a file or device to request read access.
    Otherwise, other processes cannot open the file or device if they request read access.
    If this flag is not specified, but the file or device has been opened for read access, the function fails.

    See more details in MSDN CreateFile.