How to find out if a file was modified?
Asked Answered
L

5

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.

Leupold answered 18/3, 2013 at 13:29 Comment(8)
The very concept of a last modified time is not portable.Still
Windows at least has an api call for letting you know when a file is modified. msdn.microsoft.com/en-us/library/aa365465(VS.85).aspx Maybe your other target OS have similar feature.Leaky
SUS/POSIX/IEEE1003 platforms have stat. But there is no portable C++ way.Still
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.Gwinn
@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.Still
@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.Gwinn
@Dampsquid: I agree. Watching file modification times is a hack anyway.Still
@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.Leupold
H
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

Hemipterous answered 18/3, 2013 at 13:33 Comment(1)
This is now part of the C++17 standard as std::filesystem::last_write_timeImpedimenta
S
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.

Syndetic answered 18/3, 2013 at 13:35 Comment(0)
E
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.

Eveevection answered 18/3, 2013 at 13:44 Comment(4)
It seems a bit overkill to use such a big library for this small task.Leupold
@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 :)Eveevection
I am developing an OpenGL game, therefore I do not need most of that framework. But thanks for your answer, anyway!Leupold
@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: youtube.com/watch?v=GYa5DLV6ADQEveevection
L
0

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

Lougheed answered 18/3, 2013 at 13:32 Comment(3)
Is this platform independent?Leupold
If the MKS toolkit is installed on windows then yes I believe so.Lougheed
Not platform independent. In fact, on many sytems it's possible to disable atime on a per-filesystem basis.Herbert
A
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?

Ante answered 11/2, 2015 at 0:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.