如何检查文件是否已经被用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().