简体   繁体   English

在Windows上使用C ++获取文件属性更改时间

[英]Obtain file properties change time using C++ on Windows

I need modification time , creation time and change time of file in windows using cpp. 我需要使用cpp在Windows中modification timecreation timechange time文件的change time I am using following code: 我正在使用以下代码:

string filename = "D:\\hi.txt";
struct stat result;
if (stat(filename.c_str(), &result) == 0)
{
    int a = 10;
    auto mod_time = result.st_mtime;
    cout << "modified time is: "<<mod_time<<endl;
}

Using this I am able to get modification and creation time . 使用这个我可以得到modificationcreation time But, I am not able to get change time for the file. 但是,我无法获取文件的change time How should I get change time for file using cpp? 如何使用cpp获取文件change time

The definition of "change time" follows. 以下是“更改时间”的定义。

Modification time changes when the content of the file changes and Change time changes even when the properties of the file change like access permissions. 修改时间会在文件内容更改时更改,并且更改时间也会更改,即使文件的属性(如访问权限)也会更改。

MSDN defines three timestamps for files: Creation Time, Last Access Time, Last Write Time. MSDN为文件定义了三个时间戳:创建时间,上次访问时间,上次写入时间。 What you ask for looks in fact to be the Last Access Time. 您要求的实际上是最后访问时间。

In your example you use a Libc function stat() which is meant to work on all systems that have a C compiler. 在您的示例中,您使用了Libc函数stat() ,该函数旨在在具有C编译器的所有系统上工作。 As it is, it may be too generic ie it does not represent all capabilities available inside a particular environment (MS Windows in your case), only a subset of generic properties. 实际上,它可能太通用了,即,它不代表特定环境(在您的情况下为MS Windows)内部的所有可用功能,仅代表通用属性的一部分。

At this link you can find the description of GetFileTime() WinAPI function that returns file times supported on Windows. 在此链接中,您可以找到WinAPI函数GetFileTime()的描述,该函数返回Windows支持的文件时间。 If you write an application that is not meant to be ported to other platforms, you are better off using WinAPI for system-level things. 如果编写的应用程序不打算移植到其他平台,则最好使用WinAPI进行系统级的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM