简体   繁体   English

获取C中文件的上次修改日期

[英]Getting the last modified date of a file in C

I want to get the last modified date of a file in C. Almost all sources I found use something along this snippet: 我想在C中获取文件的最后修改日期。几乎所有我发现的源都使用了这个代码段:

char *get_last_modified(char *file) {
    struct tm *clock;
    struct stat attr;

    stat(file, &attr);
    clock = gmtime(&(attr.st_mtime));

    return asctime(clock);
}

But the attr doesn't even have a field st_mtime , only st_mtimespec . 但是attr甚至没有字段st_mtime ,只有st_mtimespec Yet, when using this my Eclipse tells me that passing argument 1 of 'gmtime' from incompatible pointer type on the line clock = gmtime(&(attr.st_mtimespec)); 然而,当使用这个时,我的Eclipse告诉我在行clock = gmtime(&(attr.st_mtimespec));passing argument 1 of 'gmtime' from incompatible pointer type clock = gmtime(&(attr.st_mtimespec));

What am I doing wrong? 我究竟做错了什么?

PS: I'm developing on OSX Snow Leopard, Eclipse CDT and using GCC as Cross-Platform compiler PS:我正在开发OSX Snow Leopard,Eclipse CDT并使用GCC作为跨平台编译器

On OS X, st_mtimespec.tv_sec is the equivalent of st_mtime . 在OS X上, st_mtimespec.tv_sec等效于st_mtime

To make this portable, do 为了使这个便携,做

#ifdef __APPLE__
#ifndef st_mtime
#define st_mtime st_mtimespec.tv_sec
#endif
#endif

and then use st_mtime . 然后使用st_mtime

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

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