c++ 获取文件或文件夹的创建日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21159047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:30:38  来源:igfitidea点击:

Get the creation date of file or folder in c++

c++file

提问by user3202024

I want to write a program to get the details of files and folder I created including the creation date like this: Feb 15 2006 What should I do? Any suggestion? And I have to mentioned that I'm not allowed to use windows.h .

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

回答by the swine

In Linux-like system, you can use the stat function, as such:

在类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;
}

In Windows, I'd suggest using the system() function and get file time from commandline:

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

#include <stdlib.h>

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

    return 0;
}

You can redirect output of system() to e.g. a temporary file and parse the date from there.

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

Or use this workaround https://stackoverflow.com/a/40504396/1422630, that makes windows' _statcompatible with linux one stat, basically:

或使用此解决方法https://stackoverflow.com/a/40504396/1422630,这使 windows_stat与 linux one 兼容stat,基本上:

#ifdef WIN32
  #define stat _stat
#endif

回答by James Kanze

The simple answer is that you can't. Depending on the system, there may be a system specific function which will do this (e.g. GetFileAttributesExunder Windows), but not all systems even support it; Unix, for example, does notkeep this information, and there is no way to obtain it on a Unix system (or on a file system which is hosted on a Unix system—I don't know what the Windows function will return if the file is physically hosted on a Unix system).

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

回答by SuRendra

#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