如何检查文件是否已经被用C另一个进程打开? [英] How to check if a file is already open by another process in C?

查看:1044
本文介绍了如何检查文件是否已经被用C另一个进程打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到,标准C没有如果文件在另一个进程已经打开,告诉的方式。所以答案应包含每个平台的几个例子。我需要检查的Visual C ++ / Windows的虽然。

I see that standard C has no way of telling if a file is already opened in another process. So the answer should contain several examples for each platform. I need that check for Visual C++ / Windows though.

推荐答案

有没有办法分辨,除非其他进程明确禁止对文件的访问。在MSVC,你会用 _fsopen这样做(),指定 _SH_DENYRD 为shflag说法。有兴趣来一个文件是否被打开未上锁,否则这个概念是一个多任务操作系统上存在严重缺陷。它可能被打开微秒后,你会发现它不是。这也是Windows不具有IsFileLocked()函数的原因。

There's no way tell, unless the other process explicitly forbids access to the file. In MSVC, you'd do so with _fsopen(), specifying _SH_DENYRD for the shflag argument. The notion of being interested whether a file is opened that isn't otherwise locked is deeply flawed on a multitasking operating system. It might be opened a microsecond after you'd have found it wasn't. That's also the reason that Windows doesn't have a IsFileLocked() function.

如果您需要将文件同步访问,你需要用一个命名互斥添加此,使用CreateMutex()。

If you need synchronized access to files, you'll need to add this with a named mutex, use CreateMutex().

这篇关于如何检查文件是否已经被用C另一个进程打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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