How to Be Notified of File/Directory Change in C/C++, Ideally Using Posix

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

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

Can the operating system tell me when a new file is created?

Depends on which OS.

On Windows, the base API would be Directory Change Notifications.

Since you mention Linux in the tags, this would be the inotify API.

To add to the OS X answer, as of 10.5, you want the FSEvents API.

How to monitor a directory and read image in this directory in C++

I dont agree with the other guy who answered the question, it can be done eaisly with some co-existing libraries that I used below, This code does what you are looking for,

string processName()
{

FILETIME bestDate = { 0, 0 };
FILETIME curDate;
string name;
CFileFind finder;

BOOL bWorking = finder.FindFile("*.png");

while (bWorking)
{

bWorking = finder.FindNextFile();

// date = (((ULONGLONG) finder.GetCreationTime(ft).dwHighDateTime) << 32) + finder.GetCreationTime(ft).dwLowDateTime;

finder.GetCreationTime(&curDate);

if (CompareFileTime(&curDate, &bestDate) > 0 )
{
bestDate = curDate;
name = finder.GetFileName().GetString();
// name = (LPCTSTR) finder.GetFileName();
}

}
return name;
}

Here I wrote it for the code you, it takes the name of the last entered /.png extension you can check libray for it.

Please tell me for further question

How can I get the list of files in a directory using C or C++?

UPDATE 2017:

In C++17 there is now an official way to list files of your file system: std::filesystem. There is an excellent answer from Shreevardhan below with this source code:

#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}

Old Answer:

In small and simple tasks I do not use boost, I use dirent.h. It is available as a standard header in UNIX, and also available for Windows via a compatibility layer created by Toni Ronkko.

DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}

It is just a small header file and does most of the simple stuff you need without using a big template-based approach like boost (no offence, I like boost!).

Getting list of files in a folder using C

In POSIX operating systems, you can call opendir() and readdir(). In Windows you can call _findfirst() and _findnext(). With a little effort you can implement your own opendir() and readdir() as wrapper functions under Windows, so that your application code can use the same API everywhere. An example of that can be found here.

OS-independent API to monitor file system?

A bonified answer, albeit one that requires a largish library dependency (well-worth it IMO)!

QT provides the QFileSystemwatcher class, which uses the native mechanism of the underlying platform.

Even better, you can use the QT language bindings for Python or Ruby. Here is a simple PyQT4 application which uses QFileSystemWatcher.

Notes

  • A good reference on on creating deployable PyQT4 apps, especially on OSX but should work for Windows also.
  • Same solution previously posted here.
  • Other cross-platform toolkits may also do the trick (for example Gnome's GIO has GFileMonitor, although it is UNIX only and doesn't support OSX's FSEvents mechanism afaik).


Related Topics



Leave a reply



Submit