有一个异步的方式知道一个文件已经改变? [英] Is there an async way of knowing a file has changed?

查看:166
本文介绍了有一个异步的方式知道一个文件已经改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想异步监控文件中的任何更改。这是我想有一个回调(可能从内核)在我的程序中,当文件已被修改/删除。该文件只是一个纯文本文件。我知道可以使用轮询机制,但我正在寻找一个基于事件的解决方案。我读了关于inotify,但看起来需要修补我的内核。

I would like to asynchronously monitor a file for any changes. That is I would like to have a call back (possibly from kernel) in my program when the file has been modified/deleted. The file is just a plain text file. I know one can do this using a polling mechanism, but I am looking for an event based solution. I read about inotify, but looks like it needs patching of my kernel.

如果解决方案符合POSIX标准,则更好。

If the solution is POSIX compliant, its even better.

推荐答案

Inotify 于2005年被合并到Linux内核方案,因此除非你在一个<

Inotify was merged to the Linux kernel way back in 2005, so unless you're in a very old system, you should be able to use it out of the box.

我不认为这里有一个POSIX兼容的解决方案。 。 Mac OS X具有 FSEvents

I don't think there exists a POSIX compliant solution for this. Mac OS X has FSEvents.

另请查看手册页 for inotify。

Also check the man page for inotify.

编辑:

不知道你的约束和/或要求,也是 GFileMonitor (如果您使用Glib)(C ++绑定为 glibmm )和 QFileSystemWatcher 是你使用Qt。这些可能更跨平台友好。

Don't know about your constraints and/or requirements, but there is also GFileMonitor if you use Glib (the C++ binding is glibmm) and QFileSystemWatcher is you use Qt. Those are probably more cross-platform friendly.

这篇关于有一个异步的方式知道一个文件已经改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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