获得在C文件的最后修改日期 [英] Getting the last modified date of a file in C

查看:397
本文介绍了获得在C文件的最后修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望得到一个文件的最后修改日期C.我发现几乎所有的资源用的东西沿着这个片断:

I want to get the last modified date of a file in C. Almost all sources I found use something along this snippet:

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

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

    return asctime(clock);
}

ATTR 甚至没有一个字段 st_mtime ,只 st_mtimespec 。然而,使用这种时候,我的Eclipse告诉我,上线传递从兼容的指针类型'gmtime的的参数1 时钟= gmtime的(及( attr.st_mtimespec));

But the attr doesn't even have a field st_mtime, only 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));

我在做什么错了?

PS:我开发的OSX雪豹,Eclipse CDT的使用GCC作为跨平台编译器

PS: I'm developing on OSX Snow Leopard, Eclipse CDT and using GCC as Cross-Platform compiler

推荐答案

在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

然后用 st_mtime

这篇关于获得在C文件的最后修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆