std::filesystem::last_write_time
来自cppreference.com
< cpp | filesystem
<tbody>
</tbody>
| 在标头 <filesystem> 定义
|
||
std::filesystem::file_time_type last_write_time( const std::filesystem::path& p ); |
(1) | (C++17 起) |
std::filesystem::file_time_type last_write_time( const std::filesystem::path& p, std::error_code& ec ) noexcept; |
(2) | (C++17 起) |
void last_write_time( const std::filesystem::path& p, std::filesystem::file_time_type new_time ); |
(3) | (C++17 起) |
void last_write_time( const std::filesystem::path& p, std::filesystem::file_time_type new_time, std::error_code& ec ) noexcept; |
(4) | (C++17 起) |
参数
| p | - | 要检验或修改的路径 |
| new_time | - | 新的修改时间 |
| ec | - | 不抛出重载中报告错误的输出形参 |
返回值
1)
p 的最后修改时间。2) (无)
异常
若内存分配失败,则任何不标记为 noexcept 的重载可能抛出 std::bad_alloc 。
1,3) 抛出 std::filesystem::filesystem_error,构造时以
p 为第一路径实参并以OS 错误码为错误码实参。注解
不保证在设置写入时间后,(1,2) 的返回值立即等于传递给 (3,4) 的实参,因为文件系统时间可能比 std::filesystem::file_time_type 具有更大粒度。
示例
运行此代码
#include <chrono>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
using namespace std::chrono_literals;
int main()
{
auto p = std::filesystem::temp_directory_path() / "example.bin";
std::ofstream{p.c_str()}.put('a'); // 创建文件
std::filesystem::file_time_type ftime = std::filesystem::last_write_time(p);
std::cout << std::format("File write time is {}\n", ftime);
// 文件写入时间向未来移动 1 小时
std::filesystem::last_write_time(p, ftime + 1h);
// 从文件系统回读
ftime = std::filesystem::last_write_time(p);
std::cout << std::format("File write time is {}\n", ftime);
std::filesystem::remove(p);
}
可能的输出:
File write time is 2023-09-04 19:33:24.702639224
File write time is 2023-09-04 20:33:24.702639224
参阅
(C++17) |
表示文件时间值 (typedef) |
| 获取或设置 directory_entry 所代表的文件的最后数据修改时间 ( std::filesystem::directory_entry 的公开成员函数)
|