std::filesystem::last_write_time是获取文件最后修改时间的首选方法,返回file_time_type类型,需转为system_clock::time_point才能格式化输出,且必须启用c++17及以上标准。

std::filesystem::last_write_time 获取修改时间最直接
想拿文件最后修改时间,std::filesystem::last_write_time 是首选,它返回 std::filesystem::file_time_type,本质是高精度时钟点(通常对应系统 FAT/NTFS/ext4 的 mtime)。别试图用 status() 解包时间戳——它不存时间信息,只管权限、类型、存在性。
常见错误:调用 std::filesystem::status("path").permissions() 后以为能顺手拿到时间,结果编译失败或返回随机值。时间戳必须走专用函数。
- 必须开启 C++17 或更高标准(
-std=c++17) - 路径需为绝对路径或确保当前工作目录正确,相对路径出错时不会报“文件不存在”,而是抛
std::filesystem::filesystem_error - 返回值不能直接 cout,要先转成
std::chrono::system_clock::time_point才能格式化输出
auto ftime = std::filesystem::last_write_time("config.json");
auto sctp = std::chrono::clock_cast<:chrono::system_clock>(ftime);
std::cout
<h3>std::filesystem::status 仅用于判断文件元信息,不是时间接口</h3>
<p><code>std::filesystem::status</code> 的作用是快速获取文件是否存在、是否为目录、是否可执行等,底层调用 <code>stat()</code> 系统调用但**不缓存也不暴露其中的 <code>st_mtime</code> 字段**。它的返回值 <code>std::filesystem::file_status</code> 没有公开时间访问方法。</p>
<p>典型误用场景:在循环里反复调用 <code>status()</code> 检查文件变化,却期望从中提取时间做轮询对比——这既低效又不可靠,应改用 <code>last_write_time</code> 配合 <code>file_time_type::operator!=</code> 比较。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/gongju/2823" title="C++14"><img
src="https://img.php.cn/upload/manual/001/431/639/6ac8b33c327c4749.png" alt="C++14" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="overflowclass">C++14</a>
<p class="overflowclass">C++14 对 C++11 的修正与增强版本,适合旧系统维护和较老工具链兼容。</p>
</div>
<a rel="nofollow" href="/xiazai/gongju/2823" title="C++14" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<ul>
<li>
<code>status()</code> 可能触发 symlink 解析(取决于重载版本),若路径含软链且目标不存在,会抛异常;而 <code>last_write_time</code> 对 dangling symlink 默认失败</li>
<li>若只需判断文件是否存在且非目录,<code>is_regular_file(status(path))</code> 比 <code>exists()</code> + <code>is_directory()</code> 少一次系统调用</li>
</ul>
<h3>跨平台时间精度差异必须手动处理</h3>
<p>Windows NTFS 默认提供 100ns 精度,Linux ext4 通常是 1s(老内核)或 1ns(带 <code>CONFIG_FS_EXT4_USE_CREDITS</code>),但 <code>std::filesystem::file_time_type</code> 在各 STL 实现中映射不同:<code>libstdc++</code> 用 <code>__gnu_cxx::__static_assert</code> 保证与 <code>system_clock</code> 兼容,<code>libc++</code> 则可能截断纳秒位。直接比较两个文件的 <code>last_write_time</code> 结果,在某些环境下可能因舍入导致“本该相等却判为不等”。</p>
<ul>
<li>如需稳定比较,统一转成 <code>std::chrono::seconds</code> 再比: <code>auto sec_a = time_a.time_since_epoch() / 1s;</code>
</li>
<li>写日志或 UI 显示时,优先用 <code>std::format("{:%F %T}", sctp)</code>(C++20),避免 <code>localtime</code> 的线程不安全问题</li>
<li>注意:<code>last_write_time</code> 不等于“创建时间”——Windows 有 <code>creation_time</code>(需 WinAPI),Linux 一般无可靠 birth time</li>
</ul>
<h3>权限不足或符号链接失效时的错误处理要点</h3>
<p><code>last_write_time</code> 和 <code>status</code> 都会在权限不足(如只读挂载)、路径不存在、symlink 目标失效时抛 <code>std::filesystem::filesystem_error</code>。但两者的 errno 映射不同:<code>last_write_time</code> 在 Linux 上常映射 <code>ENOENT</code> 或 <code>EACCES</code>,而 <code>status</code> 对 dangling symlink 可能返回 <code>ENOTDIR</code>。</p>
<ul>
<li>不要用 <code>try/catch</code> 包裹单次调用就完事,生产代码应预检:先 <code>exists()</code>,再 <code>is_regular_file()</code>,最后取时间</li>
<li>对用户传入路径,建议用 <code>std::filesystem::weakly_canonical</code> 规范化,避免 <code>../</code> 导致权限检查绕过</li>
<li>捕获异常后,<code>e.code().value()</code> 比 <code>e.what()</code> 更适合做分支逻辑(例如区分 <code>EPERM</code> 和 <code>ESTALE</code>)</li>
</ul>
<p>真正麻烦的是 NFS 或 FUSE 文件系统——<code>last_write_time</code> 可能返回陈旧值,且无法通过 STL 接口感知缓存状态。这种场景下,得退回到 <code>stat()</code> 系统调用并检查 <code>st_ctime</code> 和 <code>st_mtim.tv_nsec</code> 原始字段。</p></:chrono::system_clock>C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










