【问题标题】:How to allow other programs to read file, while writing to it using fopen and fwrite?如何允许其他程序读取文件,同时使用 fopen 和 fwrite 写入文件?
【发布时间】:2020-04-14 16:24:28
【问题描述】:

我正在为我正在创建的视频打开一个文件,并使用 C++ 中的 fopen 写入磁盘,我可以写入磁盘。但是当我在写它的时候尝试读取它时,它会抛出错误,说它没有权限读取文件,只要我关闭文件或停止程序,我就可以突然读取它。

如果程序崩溃,没有完成写入不是问题,仍然可以读取它。此外,VLC 的日志告诉我这是权限问题。

知道如何更改该权限吗?

回复 William 询问代码 sn-ps 或是否在文件存在之前打开:

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

【问题讨论】:

  • 没有读取文件的权限,或者没有打开文件的权限?显示一些代码。 fopen 应该会成功。如果您尝试使用 VLC 等大型设备读取文件,则很可能在文件存在之前打开,您看到的错误基本上是由于缓存无效。
  • 谢谢威廉,这就是我所拥有的。我等了几分钟,到那时可以用 Windows 资源管理器看到该文件,然后等到我刷新并且数据在那里之后,无法使用 VLC 或 Notepad++ 或记事本或 Windows Media Player 打开记事本说无法访问,因为它是被另一个进程使用,其他进程也被使用。这是 VLC 日志,它试图打开它:snippi.com/s/g4cbu23 这是我用 fopen 创建文件的地方:snippi.com/s/cyajw4h 最后是我使用 fwrite 和刷新写入文件的地方:snippi.com/s/oz27m0g
  • 这能回答你的问题吗? C++ : Opening a file in non exclusive mode

标签: c++ fopen


【解决方案1】:

如果您希望文件可共享,则需要使用 _fsopen_SH_DENYNO

【讨论】:

  • 谢谢,这成功了。是否需要以类似的方式打开文件进行读取?我现在可以打开文件以使用大多数程序进行阅读。只是不能使用我们的库。
  • @MatthewCzarnek 我相信是的。另一个奇怪的怪癖是,许多您可能认为写入它们打开的文件的程序实际上并没有。例如,许多编辑写出文件的新副本,然后用新文件替换旧文件,从不写入旧文件。
猜你喜欢
  • 2020-07-05
  • 2016-05-07
  • 1970-01-01
  • 2023-03-09
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2020-08-14
相关资源
最近更新 更多