I would like to touch my files from C code to modify their access date. This does not seem to work:
struct stat fileSt;
lstat(path, &fileSt);
fileSt.st_mtime = time(NULL);
Thank you for help.
The touch command changes certain dates for each file argument. By default, touch sets both the date of last file modification and the date of last file access to the current time.
The modified timestamp contains the time the file's data has been changed. It means when we make changes to the actual contents of the file, the file system will update the modification time.
The old utime()
and utimes()
are OK for many use-cases, but to update atime
& mtime
with nanosecond resolution, which you need on modern systems, use:
utimensat(0, path, NULL, 0);
This is very useful in combination with newer stat()
which returns a struct timespec
st_mtim
field in struct stat
with nanosecond resolution as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With