'C++: mtime: ls and mtime don't match
I'm encountering a situation where I think the mtime returned by ls on the command line differs from the mtime returned by calling stat in my C++ program. Does anyone know why this might be the case?
Because this is a university assignment, I can't share my exact code with you. However, this is essentially what I am doing in my program:
DIR *dr;
struct dirent *mydirent;
dr = opendir(somepath);
if (dr) {
while((mydirent = readdir(dr)) != NULL){
struct stat sb;
stat(mydirent->d_name, &sb);
fprintf(stdout, "%s %d\n", mydirent->d_name, (int)sb.st_mtime);
}//while
}//if
closedir(dr);
Here's some sample output from my program. I'm focused mainly on the mtime of the file test.txt.
vagrant@vagrant:~/gios-spr-22-pr4$ ls --time-style='+%s' -l ./mnt/server
total 876
-rw-rw-r-- 1 vagrant vagrant 886443 1649448731 gt-einsteing2.jpg
drwxrwxr-x 2 vagrant vagrant 4096 1649535341 sample-files
-rw-rw-r-- 1 vagrant vagrant 1024 **1649535794 test.txt**
vagrant@vagrant:~/gios-spr-22-pr4$ ./bin/dfs-server-p1
-- SYSINFO: DFSServerNode server listening on 0.0.0.0:42001
.. 1649616999
**test.txt 1649535767**
.gitignore 1648919848
. 1649535767
In short, why is ls and stat returning different mtimes for test.txt?
Thanks, Ben
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
