I thought I'd post an example for the UNIX method of detecting that a file changed. Well, this actually runs on Windows. This turns out to be REALLY easy to pull off! Watch out for the argv[1] parameter for _stat64 if you run it on a compiler other than Visual Studio. Try it with any file. A text file works, as does image files. Every time you save, it should print File changed!

I used the Windows Sleep command to pause the loop every 500 ms, half of a second, so that I'm just not hammering the file. I haven't tested the UNIX version.

#include <windows.h> // for Windows Sleep
// #include <unistd.h> // for UNIX sleep
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>

/*
   // st_mtime is the last time the time has changed

   // thanks to the thread at
   // http://stackoverflow.com/questions/231746/how-do-i-monitor-text-file-changes-with-c-difficulty-no-net
   // and Adam Pierce for his tip and lead
   //
   // Also, see http://publib.boulder.ibm.com/iseries/v5r1/ic2924/index.htm?info/apis/stat64.htm
   //       and http://msdn.microsoft.com/en-us/library/14h5k7ff%28VS.71%29.aspx
*/

int main(int argc, char **argv)
{
   struct __stat64 fileinfo;
   time_t origTime;

   if ( argc != 2 )
   {
      printf("Syntax:\n\n%s <File to monitor. Use Quotes for filenames with spaces>.", argv[0]);
      exit(0);
   }

   // get the file stats
   //
   // 0  == success
   // -1 == fail
   if ( -1 == _stat64(argv[1], &fileinfo)  )
   {
      printf("Error accessing file.\n");
      exit(0);
   }

   // reads the last modified time from the file attributes
   origTime = fileinfo.st_mtime;

   // you'll have to ctrl-C to exit, but keep polling the file to see if it has changed
   // note that this function also keeps track of the new "last changed" file information
   while (true)
   {
      if(-1 != _stat64(argv[1], &fileinfo) )
      {
         if ( origTime != fileinfo.st_mtime )
         {
            printf("File changed!\n");
            origTime = fileinfo.st_mtime;
         }
      }

      // note that Sleep() is the Windows version of sleep, which counts by miliseconds.
      // The UNIX one counts by seconds
     
      Sleep(500);  // for Windows
      // sleep(1); // for UNIX
   }

   return 42;
}