rustyx
rustyx

Reputation: 85361

Portable way to set file modification time in C++?

I'm writing a C++ application that needs to be portable between at least Ubuntu 15, FreeBSD 11, MacOS X and Windows 7 (compiled with GCC, clang and MSVC). All of these systems have a notion of a file with at least a modification date/time.

The question is: is there a way to set a file modification time using a single piece of C++ code that would work on all of these systems?

NB: By setting the file modification time I mean setting it to any specified value, not the current time (i.e. not the touch functionality).

Upvotes: 3

Views: 2302

Answers (2)

Claudio
Claudio

Reputation: 10947

The C++ Standard Library does not (yet) have any notion of file (until filesystem will become part of the Standard Library, which may happen with C++17). At the moment, you need to rely on some cross-platform library such as boost::filesystem or Qt.

Upvotes: 4

Christophe
Christophe

Reputation: 73376

With boost::filesystem, you can use this function to get/set the time:

void last_write_time(const path& p, const std::time_t new_time);

This is also available on some compilers as experimental feature on some compiles such as gcc or msvc. These use same API but different experimental namespace. So if you don't want to use boost, and as long as it's not yet integrated in the standard, you could make it portable with conditional compilation and using statement.

Upvotes: 7

Related Questions