【问题标题】:How to get creation time and last write time of a file如何获取文件的创建时间和最后写入时间
【发布时间】:2012-11-01 09:15:07
【问题描述】:

我正在打开几个文件,并想在我的结构中添加文件的创建时间和最后一次写入时间。有什么办法可以吗?

【问题讨论】:

  • 根据操作系统和文件系统,可能根本不存储创建时间。

标签: c time system


【解决方案1】:

您可以使用fstat 读取上次写入时间(请参阅stat.st_mtime)。

我不知道读取创建时间的可移植方式。在 Windows 上,您可以使用 GetFileTime

【讨论】:

    【解决方案2】:

    使用stat()

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    int stat(const char *path, struct stat *buf);
    

    struct stat 包含几个描述时间和日期信息的字段,特别是

    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 */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2017-12-16
      • 2010-09-19
      • 2013-01-01
      • 2013-02-13
      • 2012-11-21
      相关资源
      最近更新 更多