在有人尝试写入文件之前,如何读取文件?
我的文件系统中有一个文件,我需要我的程序尽可能频繁地读取该文件。当文件发生更改时,它会将所有更改复制到缓冲区,然后清空文件。
该文件偶尔会被其他程序写入。
问题是,您不能同时读取和写入一个文件。如何确保我的程序尽可能频繁地检查文件,同时仍允许其他程序写入该文件?
该程序不会是多平台的,因此我希望我的代码尽可能依赖于 Windows API,以减小二进制文件的大小。
谢谢!
I have a file in my filesystem that I need my program to read as frequently as possible. When the file changes, it will copy all the changes to a buffer and then empty the file.
This file is going to be written to occasionally by some other program.
The thing is, you can't both read from and write to one file at the same time. How do I make sure that my program checks the file as frequently as possible while still allowing other programs to write to it?
This program will not be multi-platform so I'd like to keep my code as dependent on the Windows API as possible to keep binary size down.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 FindFirstChangeNotification() API。除其他事项外,它还可以让您知道文件何时发生更改。
Try the FindFirstChangeNotification() API. It lets you know when a file is changed, among other things.
正确的方法是使用过滤驱动程序,而不是让数据进入文件,而是动态捕获它们并使用它们。如果您有文件系统过滤器驱动程序(我们有;),那么这是一项微不足道的任务。所有其他方法都容易跳过数据或反应太慢或导致共享冲突(取决于将数据写入文件的应用程序)。
The right approach would be to use a filter driver and not let the data get into the file, instead capturing them on the fly and using them. A trivial task if you have a filesystem filter driver (we do ;). All other approaches are prone to either skipping data or reacting too slow or causing sharing violations (depending on what application writes the data to the file).