gpt4 book ai didi

C++如何获取文件最近更改的时间戳?

转载 作者:行者123 更新时间:2023-11-30 05:17:51 31 4
gpt4 key购买 nike

我想确定 Windows 上文件最近一次更改的时间戳。

是的,我已经将 stat 函数与 st_atime、st_mtime、st_ctime 一起使用了。但是,Windows 不会为特定文件更新这些时间戳。 (我不想改变这种行为,因为稍后这将是特定于客户的)。

那么,我怎样才能确定文件最近一次更改的时间戳?

例如

  • 重命名文件名的时间戳(目前无法正常工作)

  • 修改文件的时间戳(mctime 提供了这个,但我更喜欢单向解决方案)

提前致谢。

最佳答案

如果您使用的是 Windows,请使用 GetFileTime ( https://msdn.microsoft.com/en-us/library/windows/desktop/ms724320(v=vs.85).aspx ),您已通过 CreateFile ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx ) 打开文件。

为此,我强烈推荐阅读文章“检索上次写入时间”:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724926(v=vs.85).aspx

编辑您的评论:

在您应用的包含部分:

#include <string>
#include <sstream>
#include <iostream>

在方法 GetLastWriteTime 中从链接而不是 StringCchPrintf 添加:

// Build a string showing the date and time.
std::stringstream ss;
ss << stLocal.wYear << "-" << stLocal.wMonth << "-" << stLocal.wDay << " " << stLocal.wHour << ":" << stLocal.wMinute ;
std::string timeString = ss.str();
std::cout << timeString;

请阅读文档:http://www.cprogramming.com/tutorial/c++-iostreams.html然后是来自 http://en.cppreference.com/w/cpp/io/basic_stringstream 的引用

关于C++如何获取文件最近更改的时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42023449/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com