简体   繁体   English

如何在 OS X/C 中获取文件的最后修改时间?

[英]How to get last modified time of a file in OS X/ C?

I have the following code:我有以下代码:

struct stat info;
stat("/Users/john/test.txt", &info);
printf("%s\n", ctime(&info.st_mtimespec));

in which I am trying to get the last modified time of the file as displayed in the ls -l command in the format:其中我试图获取文件的最后修改时间,如ls -l命令中显示的格式:

Jan 29 19:39

Unfortunately, above code doesn't work.I get the following error message on xcode:不幸的是,上面的代码不起作用。我在 xcode 上收到以下错误消息:

Conflicting types for ctime

How can I fix it ?我该如何解决? If there any alternative approaches to get the time in format mentioned, please do mention.如果有任何其他方法可以以提到的格式获取时间,请务必提及。

Checking on struct stat 's declaration you'll see that st_mtimespec must be st_mtime .检查struct stat的声明,您会看到st_mtimespec必须是st_mtime

Then, basing on this question , I rearranged your code like this:然后,基于这个问题,我像这样重新排列了你的代码:

struct stat info;
struct tm* tm_info;
char buffer[16];

stat("/Users/john/test.txt", &info);
tm_info = localtime(&info.st_mtime);
strftime(buffer, sizeof(buffer), "%b %d %H:%M", tm_info);

printf("%s\n", buffer);

Hope it helps.希望能帮助到你。

I believe this is what you are looking for:我相信这就是你要找的:

#include <sys/stat.h>
#include <time.h>

int main(int argc, char *argv[])
{
    struct stat info;
    stat("sample.txt", &info);
    printf("%.12s\n", 4+ctime(&info.st_mtimespec));
    return 0;
}

Output( same as time field of ls -l ):输出(与ls -l时间字段相同):

Feb  4 00:43

(This was for a random file on my computer). (这是我电脑上的一个随机文件)。

does your code have:你的代码有:

#include <time.h>

also, the ctime() function expects the passed parameter to be a POINTER to a time_t .此外, ctime()函数期望传递的参数是指向time_t的 POINTER。

Here is the struct pointed to by the stat() function:这是stat()函数指向的结构体:

          struct stat {
           dev_t     st_dev;     /* ID of device containing file */
           ino_t     st_ino;     /* inode number */
           mode_t    st_mode;    /* protection */
           nlink_t   st_nlink;   /* number of hard links */
           uid_t     st_uid;     /* user ID of owner */
           gid_t     st_gid;     /* group ID of owner */
           dev_t     st_rdev;    /* device ID (if special file) */
           off_t     st_size;    /* total size, in bytes */
           blksize_t st_blksize; /* blocksize for filesystem I/O */
           blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
           time_t    st_atime;   /* time of last access */
           time_t    st_mtime;   /* time of last modification */
           time_t    st_ctime;   /* time of last status change */
       };

Notice that none of the fields are a st_mtimespec请注意,所有字段都不是st_mtimespec

perhaps you meant st_mtime也许你的意思是st_mtime

Note: your running OS-X and I'm running linux, but the OS-X should have the very same definitions of field names.注意:您运行的 OS-X 和我运行的 linux,但 OS-X 应该具有完全相同的字段名称定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM