Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be notified of file/directory change in C/C++, ideally using POSIX

Tags:

c++

c

posix

The subject says it all - normally easy and cross platform way is to poll, intelligently. But every OS has some means to notify without polling. Is it possible in a reasonably cross platform way? (I only really care about Windows and Linux, but I use mac, so I thought posix may help?)

like image 935
Michael Neale Avatar asked Sep 14 '08 10:09

Michael Neale


2 Answers

Linux users can use inotify

inotify is a Linux kernel subsystem that provides file system event notification.

Some goodies for Windows fellows:

  • File Change Notification on MSDN
  • "When Folders Change" article
  • File System Notification on Change
like image 86
aku Avatar answered Oct 26 '22 20:10

aku


The Qt library has a QFileSystemWatcher class which provides cross platform notifications when a file changes. Even if you are not using Qt, because the source is available you could have a look at it as a sample for your own implementation. Qt has separate implementations for Windows, Linux and Mac.

like image 20
David Dibben Avatar answered Oct 26 '22 22:10

David Dibben