fopen 用于写入但不是排他的 [英] fopen for writing but not exclusive

查看:21
本文介绍了fopen 用于写入但不是排他的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 fopen("file", "w") 打开一个文件进行写入但不是排他的.IE.我想让另一个进程在文件仍然打开时读取它.

I want to use fopen("file", "w") to open a file for writing but not exclusive. I.e. I want to have another process read the file while it is still open.

请注意,我在每一行之后都有一个同花顺,所以我不会错过任何东西.在其他进程从文件中读取时,写入将处于空闲状态.

Note that I have a flush after every line so I won't miss anything. Writing will be idle at the time the other process reads from the file.

文档没有提到排他性,但实验表明它是排他性的.

The documentation does not mention exclusive but experimenting shows that it is exclusive.

有办法吗?

推荐答案

文件的共享访问是特定于操作系统的功能.fopen 太通用了,不提供那种控制.你需要使用更具体的东西.如果您使用的是微软平台(假设您标记为 VS 2015),您可以使用 _fsopen 或 _wfsopen - 他们有第三个参数来指定共享访问.

Shared access on files is an OS-specific feature. fopen is too generic and does not provide that kind of control. You would need to use something more specific. If you are using a microsoft platform (assuming since you tagged for VS 2015) you can use _fsopen or _wfsopen - they have a third parameter to specify the shared access.

_fsopen("file", "w", _SH_DENYWR);

这将打开文件进行写入并允许其他人从中读取(但不能写入).

this will open the file for writing and allow others to read from it (but not write to it).

参数 shflag 是一个常量表达式,由以下之一组成以下清单常量,在 Share.h 中定义.

The argument shflag is a constant expression consisting of one of the following manifest constants, defined in Share.h.

  • 术语定义
  • _SH_COMPAT 设置 16 位应用程序的兼容模式.
  • _SH_DENYNO 允许读写访问.
  • _SH_DENYRD 拒绝读取文件.
  • _SH_DENYRW 拒绝对文件进行读写访问.
  • _SH_DENYWR 拒绝对文件的写访问.

其他操作系统/平台也可能支持 fsopen 的某些变体.

Other OS/platforms may support some variation of fsopen too.

这篇关于fopen 用于写入但不是排他的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆