In the process of software development, I encountered a requirement to obtain the time stamps of file creation time, modification time and access time. It is easy to obtain this information by calling struct stat related interfaces, but the requirement is to obtain nanosecond timestamps. So I don't know what to do, is there someone give me a solution? By the way, I use C++ programming under Linux.
Asked
Active
Viewed 376 times
1
-
Does this answer your question? [Timer function to provide time in nano seconds using C++](https://stackoverflow.com/questions/275004/timer-function-to-provide-time-in-nano-seconds-using-c) – voiarn Jan 13 '22 at 10:04
-
Keep in mind that even fast flash memory takes ~250 ms per write. Modern flash controllers are very good at having multiple overlapping writes going on at the same time, but physics takes hundreds of millions of nanoseconds. – MSalters Jan 13 '22 at 11:41
1 Answers
3
Not all file systems support this (ext4 does):
struct stat result;
if (stat(filename, &result) == 0) {
printf("Last access of %s: %sns: %ld\n",
filename,
ctime(&result.st_ctim.tv_sec),
result.st_ctim.tv_nsec
);
} else {
fprintf(stderr, "%s: %s.\n", argv[0], strerror(errno));
}

mmomtchev
- 2,497
- 1
- 8
- 23