Search code examples
c++fopen

How to allow other programs to read file, while writing to it using fopen and fwrite?


I'm opening a file for a video I'm creating and writing to disk with fopen in C++, I'm able to write to disk. But when I try to read it as I'm writing it, it will throw errors saying that it doesn't have permission to read the file as soon as I close the file or stop the program, I can suddenly read from it.

Not an issue with not finishing writing the write as if I crash the program, can still read it. Also, VLC's logs tell me it's a permission issue.

Any idea how to change that permission?

Response to William asking for code snippets or if open happened before the file existed:

Thanks William, here's what I've got. I waited a few minutes and could see the file with windows explorer by that point and waited until after I'd flushed and data was there, couldn't open with VLC or Notepad++ or Notepad or Windows Media Player

Notepad says cannot access because it is being used by another process, others too.
Here is the VLC log while it tries to open this:
http://snippi.com/s/g4cbu23

Here is where I create the file with fopen:
http://snippi.com/s/cyajw4h

At the very end is where I write to the file using fwrite and flush:
http://snippi.com/s/oz27m0g

Solution

  • You need to use _fsopen with _SH_DENYNO if you want the file to be shareable.