0

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
Matthew Czarnek
  • 341
  • 3
  • 13
  • Doesn't have permission to read the file, or doesn't have permission to open the file? Show some code. `fopen` should succeed. If you're trying to read the file using some large apparatus like VLC, quite possibly the open happened before the file existed, and the error you are seeing is bascially due to an invalid cache. – William Pursell Apr 14 '20 at 16:33
  • 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 – Matthew Czarnek Apr 14 '20 at 16:55
  • 1
    Does this answer your question? [C++ : Opening a file in non exclusive mode](https://stackoverflow.com/questions/27700/c-opening-a-file-in-non-exclusive-mode) – Mikel Rychliski Apr 14 '20 at 17:00

1 Answers1

1

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

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Thank you, this did the trick. Does the file need to be opened for reading in a similar fashion? I can now open the file for reading with most programs.. just not with our library. – Matthew Czarnek Apr 15 '20 at 14:47
  • 1
    @MatthewCzarnek I believe so. Another weird quirk is that a lot of programs that you might think write to files they open really don't. For example, many editors write out a new copy of the file and then replace the old file with the new, never writing to the old file. – David Schwartz Apr 15 '20 at 15:31