【问题标题】:Get the creation date of file or folder in c++获取C++中文件或文件夹的创建日期
【发布时间】:2014-01-16 10:22:13
【问题描述】:

我想编写一个程序来获取我创建的文件和文件夹的详细信息,包括创建日期,如下所示: 2006 年 2 月 15 日 我该怎么办?有什么建议吗? 而且我必须提到我不允许使用 windows.h 。

【问题讨论】:

  • 不允许是什么意思?
  • @starsplusplus 他可能的意思是代码必须是可移植的。在这种情况下,没有解决办法。
  • @JamesKanze 更有可能的是,这是一个计算机实验室练习,或者这是任务规范,或者该实验室确实在 Windows 以外的其他操作系统中。
  • @theswine 这可能是一个计算机实验室的练习,但如果我在运行一个计算机实验室,我会坚持使用可移植代码(因此没有<windows.h>),无论平台是什么。
  • @theswine 确切。这意味着我不需要它作为实验室的家庭作业。 (或者......在高级课程中,我可能需要这样的东西来教人们如何处理系统依赖关系。但这意味着在 Windows 代码中包含 <windows.h>,并提供某种不需要信息的机制它不可用。仅适用于相当高级的课程。)

标签: c++ file


【解决方案1】:

在类Linux系统中,可以使用stat函数,如下:

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

int main(int argc, char **argv)
{
    struct stat t_stat;
    stat("file_name", &t_stat);
    struct tm * timeinfo = localtime(&t_stat.st_ctime); // or gmtime() depending on what you want
    printf("File time and date: %s", asctime(timeinfo));

    return 0;
}

在 Windows 中,我建议使用 system() 函数并从命令行获取文件时间:

#include <stdlib.h>

int main(int argc, char **argv)
{
    system("dir /T:C file_name");

    return 0;
}

您可以将 system() 的输出重定向到例如一个临时文件并从那里解析日期。

或者使用这个解决方法https://stackoverflow.com/a/40504396/1422630,这使得windows的_stat与linux兼容stat,基本上:

#ifdef WIN32
  #define stat _stat
#endif

【讨论】:

  • 在 Windows 中,没有理由不使用 GetFileAttributesEx。他不能使用&lt;windows.h&gt; 的事实可能意味着他需要一个可以在非Windows 平台上运行的解决方案。这是不可能的,因为并非所有系统都维护这些信息。
  • @JamesKanze 好吧,我会使用它,但由于他要求不使用 windows.h,这只是另一种方法。他没有说他想要哪个平台,所以我提供了适用于 linux/mac/windows 的解决方案,他可以选择一个。无论如何,人们会使用一些多平台库,其中此类功能将使用统一接口封装,当然,对于每个平台,如果定义(或在构建系统中配置)不同,实现将是适当的。
  • 花了 3 小时来了解 stat() 与 file not found 一起失败,但这个答案还是有帮助的,谢谢 :)
  • struct stat 有成员 st_ctim,而不是 st_ctime。
  • 一般来说,依赖从程序中执行命令并解析 IMO 的结果是不好的做法。
【解决方案2】:

简单的答案是你不能。根据系统, 可能有一个系统特定的功能可以做到这一点 (例如 Windows 下的GetFileAttributesEx),但不是所有系统 甚至支持它;例如,Unix not 是否保留此 信息,在 Unix 系统上无法获取 (或托管在 Unix 系统上的文件系统上——我 不知道如果文件是 Windows 函数将返回什么 物理上托管在 Unix 系统上)。

【讨论】:

  • 大多数 Unix 文件系统不存储创建时间,尽管一些,例如 HFS+、ZFS 和 UFS2 会这样做。 (维基)
  • @theswine 有趣。 Posix 不支持它。除此之外,我最近(过去 15 年)使用 Unix 的经验仅限于 Solaris 和 Linux,所以我不知道其他地方的扩展。
  • @JamesKenze 我很确定它可以在 Ubuntu、FreeBSD、Red Hat 和 Cent OS 中运行。好吧,至少可以确定st_ctime 字段在那里,不太确定它会包含什么。通常需要st_mtime,它几乎可以在任何地方使用。
  • @theswine 我承认我的“手册页”是 Posix 标准,即使我在 Linux 下工作。 (我在 Linux 下做的工作很少;我主要使用 Solaris。)
  • 大家好。 “我不允许使用 windows.h” 我的意思是这是我的作业,我的老师告诉我不要使用 windows.h 和 system.h。那你有什么建议我?而且我是用windows编译的,不是Linux之类的操作系统。
【解决方案3】:
#include <sys/stat.h>

#include <unistd.h>

#include <time.h>



struct tm* clock;               // create a time structure

struct stat attrib;         // create a file attribute structure

stat("afile.txt", &attrib);     // get the attributes of afile.txt

clock = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure



// clock->tm_year returns the year (since 1900)

// clock->tm_mon returns the month (January = 0)

// clock->tm_mday returns the day of the month

【讨论】:

  • 这是一个纯粹的 Unix 解决方案,不可移植。它返回最后修改时间,而不是创建时间(在 Unix 系统上不可用)。
  • @JamesKanze #ifdef WIN32 #define stat _stat #endif
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 2018-06-07
  • 2016-11-09
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多