C++ How to check the last modified time of a file
Asked Answered
Q

6

36

I'm caching some information from a file and I want to be able to check periodically if the file's content has been modified so that I can read the file again to get the new content if needed.

That's why I'm wondering if there is a way to get a file's last modified time in C++.

Quarrier answered 9/11, 2016 at 10:4 Comment(3)
what's wrong with good old C stat call?Vierra
@Jean-FrançoisFabre, it's a unix system call, so it's not cross-platformCorbicula
@Corbicula that's why I did not answer.Vierra
N
42

There is no language-specific way to do this, however the OS provides the required functionality. In a unix system, the stat function is what you need. There is an equivalent _stat function provided for windows under Visual Studio.

So here is code that would work for both:

#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif

#ifdef WIN32
#define stat _stat
#endif

auto filename = "/path/to/file";
struct stat result;
if(stat(filename.c_str(), &result)==0)
{
    auto mod_time = result.st_mtime;
    ...
}
Neel answered 9/11, 2016 at 10:10 Comment(3)
So there's nothing that I can use that will work both on UNIX and on Windows? :/Quarrier
Modified to add windows supportNeel
this worked great after blind coding waiting 10min to let travisci end remote compilation :)Frontolysis
C
28

since the time of this post, c++17 has been released, and it includes a filesystem library based on the boost filesystem library:

https://en.cppreference.com/w/cpp/header/filesystem

which includes a way to get the last modification time:

https://en.cppreference.com/w/cpp/filesystem/last_write_time

Cockney answered 9/4, 2019 at 4:22 Comment(2)
In order to use std::filesystem on macOS with Apple's developer tools, you must assume Catalina or later.Mantissa
The returned time is unusable portably in C++17. But from C++20, we can cast between chrono clocks, and convert the returned time to a systime which is portable.Indefectible
I
9

You can use boost's last_write_time for that. Boost is cross platform.

Here's the tutorial link for that.

Boost has the advantage that it works for all kinds of file names, so it takes care of non-ASCII file names.

Irmgardirmina answered 9/11, 2016 at 10:18 Comment(1)
since c++17 also as part of standard library en.cppreference.com/w/cpp/filesystem/last_write_timeHarmonica
D
2

Please note that there are some limitations:

... The [time] resolution is as low as one hour on some filesystems... During program execution, the system clock may be set to a new value by some other, possibly automatic, process ...

Dymoke answered 8/3, 2019 at 19:34 Comment(0)
D
1

Also note that after copying a file (on Windows) the copy's last_write_time is the last_write_time of the original file rather than the time the copy was created, as one would naively think.

Deice answered 27/7, 2020 at 15:42 Comment(0)
M
0

This cross-platform library (Mac, Windows, Linux) is simple to add to a project. It uses #ifdef's to compile the right implementation.

https://github.com/jameswynn/simplefilewatcher

Mantissa answered 5/10, 2020 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.