【问题标题】:Getting the last modified date of a file in C在C中获取文件的最后修改日期
【发布时间】:2012-07-07 13:54:43
【问题描述】:

我想在 C 中获取文件的最后修改日期。我发现的几乎所有来源都使用这个 sn-p 中的某些内容:

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 告诉我 passing argument 1 of 'gmtime' from incompatible pointer type 在线 clock = gmtime(&(attr.st_mtimespec));

我做错了什么?

PS:我正在 OSX Snow Leopard、Eclipse CDT 上进行开发,并使用 GCC 作为跨平台编译器

【问题讨论】:

  • 是的,否则stat 本身将不可用。
  • 虽然 OT:您不想存储 get_last_modified() 返回的引用,不是吗? asctime() 返回对静态内存的引用,其内容将在每次连续调用时被覆盖。
  • 它只被调用过一次,但如果它发生变化我会记住它:)

标签: c date file-io compiler-errors


【解决方案1】:

在 OS X 上,st_mtimespec.tv_sec 等同于 st_mtime

要使这个便携,做

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

然后使用st_mtime

【讨论】:

  • 有没有办法让这个交叉编译?该程序必须在没有任何警告和错误的情况下在 Ubuntu 上运行,但我没有要在其上编程的 VM...
猜你喜欢
  • 2019-02-26
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
相关资源
最近更新 更多