7

I want to implement a live reload of certain files. I assume that it is possible to somehow read the last-modified time of a file. That could be compared against the last time I loaded that file. I would keep the latter in memory.

How can I find out whether a file was modified since a given time? The solution should work on Windows, Mac and Linux.

Update: It seems like my question has raised some misinterpretations. To make thinks clear, I am asking about finding out if a file was modified in general. Using the last-modified time was just what first came to my mind, but I am open minded to any other solution! Unfortunately I cannot afford to open each file and compare its content, since we are talking about all textures of a video game.

danijar
  • 32,406
  • 45
  • 166
  • 297
  • 1
    The very concept of a last modified time is not portable. – David Schwartz Mar 18 '13 at 13:30
  • Windows at least has an api call for letting you know when a file is modified. http://msdn.microsoft.com/en-us/library/aa365465(VS.85).aspx Maybe your other target OS have similar feature. – RedX Mar 18 '13 at 13:31
  • SUS/POSIX/IEEE1003 platforms have [stat](http://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html). But there is no portable C++ way. – David Schwartz Mar 18 '13 at 13:32
  • The last Modified time ( in windows ) has a 2 second granularity. This prevents it from being used to reliably determine is a file has been modified. google "directory change notification" for windows, I cant comment on mac/linux. – Dampsquid Mar 18 '13 at 13:34
  • @Dampsquid: You can use it, you just have to wait at least two seconds before checking the file. You have the same issue on platforms where it has one second granularity -- it can be changed twice in the same second, so you must wait at least one second before accessing it to ensure any subsequent modification will change the last modified time. – David Schwartz Mar 18 '13 at 13:42
  • @DavidSchwartz Agreed for 99.99% of the time and probably a good enough solution for most applications. But time can be modified by other applications (GPS sync, SNTP server, etc), Change notifications are not affected by clock changes. – Dampsquid Mar 18 '13 at 13:50
  • @Dampsquid: I agree. Watching file modification times is a hack anyway. – David Schwartz Mar 18 '13 at 13:51
  • @DavidSchwartz. I am open minded to any other approach. Watching the last-modified time was just what first came to my mind. The questions is about finding out if a file was modified in general. Unfortunately I cannot open each file and compare the content. – danijar Mar 18 '13 at 16:09

5 Answers5

3

have a look at Boost.FileSystem, std::time_t last_write_time(const path&). Disclaimer: I'm not sure how portable this concept is

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
3

File-system issues are usually OS-dependent. Every OS has system calls and/or library-functions to access these information. On Windows there is GetFileTime-function, Unix/Linux offers stat, which should work for Mac, too. Maybe Java offers something, but anything else will be hard to achieve using only the standard-library. Google is your best friend.

bash.d
  • 13,029
  • 3
  • 29
  • 42
2

The QFileInfo class provides system-independent file information.

QDateTime QFileInfo::lastModified () const

Returns the date and time when the file was last modified.

Should be fairly portable, since that is the whole idea of Qt. Windows, MacOS and Linux are officially supported.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • 1
    It seems a bit overkill to use such a big library for this small task. – danijar Mar 18 '13 at 13:46
  • 1
    @sharethis - who says you must only use it for this? Qt is filled with a train load of portable functionality. BTW it does seem to be the only portable solution so far :) – dtech Mar 18 '13 at 13:47
  • I am developing an OpenGL game, therefore I do not need most of that framework. But thanks for your answer, anyway! – danijar Mar 18 '13 at 14:09
  • 1
    @sharethis - Actually Qt offers a neat abstraction to provide portable C++ API to create and use OpenGL contexts. That alone may be enough to justify using Qt, and then there is the rest of the framework... Take a look at this presentation: http://www.youtube.com/watch?v=GYa5DLV6ADQ – dtech Mar 18 '13 at 14:15
0

Use stat or fstat system calls. They yield a struct stat structure which contains the modification time in st_atimespec.

suspectus
  • 16,548
  • 8
  • 49
  • 57
0

Another solution would be to use inotify for Linux or if you are on windows maybe this will help ?

Is there anything like inotify on Windows?

Community
  • 1
  • 1
Muhammad Lukman Low
  • 8,177
  • 11
  • 44
  • 54