c++中如何使用std::filesystem::last_write_time_c++获取修改时间【汇总】
技术百科
尼克
发布时间:2026-01-19
浏览: 次 std::filesystem::last_write_time是C++17引入的获取文件最后修改时间戳的函数,返回file_time_type类型值,需通过to_sys(C++20)或系统调用兜底转换为可读时间。
std::filesystem::last_write_time 是什么函数
std::filesystem::last_write_time 是 C++17 引入的标准库函数,用于获取文件或目录的最后修改时间戳(std::filesystem::file_time_type 类型)。它不是“last_write_time_c++”——后者并不存在,属于常见拼写/记忆错误。
该函数返回的是一个高精度、与系统时钟对齐的时间点,但**不是 std::chrono::system_clock::time_point**,不能直接传给 std::put_time 或 std::format(C++20);需先转换。
如何正确获取并转换为可读时间字符串
直接调用 last_write_time 返回的是 file_time_type,而标准输出需要 system_clock::time_point。C++20

std::filesystem::file_time_type::clock::to_sys 转换;C++17 需借助平台适配或第三方辅助(如 Boost),或使用 std::chrono::file_clock(若编译器支持)。
- Clang 14+/GCC 12+ 在启用
-std=c++20时支持file_time_type::clock::to_sys - MSVC 19.30+(VS 2025 17.0+)已实现该转换
- 若编译器不支持,
file_time_type无法安全转为system_clock,强行转换可能导致负偏移或崩溃
#include#include #include #include namespace fs = std::filesystem; int main() { auto fpath = fs::path("example.txt"); if (fs::exists(fpath)) { auto tp = fs::last_write_time(fpath); // C++20 推荐方式 auto sys_tp = fs::file_time_type::clock::to_sys(tp); auto tmc = std::chrono::system_clock::to_time_t(sys_tp); std::cout << std::put_time(std::localtime(&tmc), "%Y-%m-%d %H:%M:%S") << '\n'; } }
常见错误:跨平台时间戳解析失败
Windows 和 Linux 对 file_time_type 的底层实现不同:Windows 使用 FILETIME(100ns 单位,基于 1601 年),Linux 通常用 stat.st_mtim(纳秒级,基于 1970 年)。C++ 标准要求 file_time_type 表示“文件系统原生时间”,但未强制统一 epoch —— 这意味着:
-
tp.time_since_epoch().count()在不同平台数值不可比 - 不做
to_sys()转换就直接用duration_cast到seconds可能得负值(尤其 Windows 上) - 某些旧版 libstdc++(GCC file_time_type 错误映射到
system_clock,导致 1970 年前时间显示异常
替代方案:用 stat 系统调用兜底(C++17 兼容)
如果目标环境不确定是否支持 to_sys,或需兼容 C++17 编译器,更稳妥的做法是绕过 std::filesystem,直接调用 POSIX stat 或 Windows GetFileTime:
- Linux/macOS:
struct stat st; stat(path.c_str(), &st); st.st_mtim.tv_sec - Windows:
FILETIME ft; GetFileTime(hFile, nullptr, nullptr, &ft);,再用FileTimeToSystemTime - 跨平台封装推荐用
boost::filesystem::last_write_time(返回time_t)
真正麻烦的从来不是“怎么取时间”,而是“怎么让这个时间在所有机器上都代表同一个时刻”。别迷信 std::filesystem 的跨平台承诺,尤其是涉及时间转换时,to_sys 是否可用、是否被正确实现,必须实测验证。
# ai
# 的是
# 尤其是
# windows
# 可通过
# 上都
# 不做
# mac
# 不支持
# 再用
# win
# linux
# format
# macos
# cos
# c++
# 标准库
# stream
# 字符串
# 封装
# 不确定
# Struct
# ios
# count
# 转换为
# 直接调用
# Filesystem
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- 如何使用Golang反射将map转换为struct
- Windows10如何重置此电脑_Windows1
- C++如何使用std::transform批量处理
- Python正则表达式实战_模式匹配说明【教程】
- Win10怎么关闭自动更新错误弹窗_Win10策略
- 如何使用Golang指针与接口结合_实现方法调用和
- 如何在Golang中处理URL参数_Golang
- Win11怎么关闭通知消息_屏蔽Windows 1
- Mac系统更新下载慢或失败怎么办_解决macOS升
- c++ atoi和atof函数用法_c++字符数组
- php下载安装选zip还是msi格式_两种安装包对
- c++中如何使用auto关键字_c++11类型推导
- Win11怎么关闭通知中心_Windows11系统
- php嵌入式多设备通信怎么实现_php同时管理多个
- Windows如何设置登录时的欢迎屏幕背景?(锁屏
- Windows10电脑怎么连接蓝牙设备_Win10
- MAC怎么一键隐藏桌面所有图标_MAC极简模式切换
- Win11怎么设置默认浏览器Chrome_Wind
- c++如何连接Redis c++ hiredis库
- php文件怎么变mp4保存_php输出视频流保存为
- Win11如何关闭小娜Cortana Win11禁
- Windows笔记本无法进入睡眠模式怎么办?(电源
- 如何自定义Windows终端的默认配置文件?(Po
- Go语言中CookieJar的持久化机制解析:内存
- Win10怎样卸载iTunes_Win10卸载iT
- 如何使用Golang搭建Web开发环境_快速启动H
- MAC怎么使用表情符号面板_MAC Emoji快捷
- Win11怎么关闭小组件_Win11禁用任务栏天气
- Win10系统怎么查看显卡温度_Win10任务管理
- Python并发安全问题_资源竞争说明【指导】
- 如何解决同一段404代码在不同主机上表现不一致的问
- 如何将文本文件中的竖排字符串转换为横排字符串
- php修改数据怎么改富文本_update更新htm
- 用Python构建微服务架构实践_FastAPI与
- 跨文件调用类方法怎么用_php作用域操作符与自动加
- Python迭代器生成器进阶教程_节省内存与懒加载
- Win11怎么更改系统语言为中文_Windows1
- 如何在Golang中使用闭包_封装变量与函数作用域
- 如何在Golang中理解指针比较_Golang地址
- 如何在 IIS 上为 ASP.NET 6 应用排除
- Win11怎么设置触控板手势_Windows11三
- php与c语言在嵌入式中有何区别_对比两者在硬件控
- Python网络超时处理_健壮性设计说明【指导】
- Windows Defender扫描失败怎么办_安
- Win11如何设置计划任务 Win11定时执行程序
- c++中如何求一个数的平方根_c++ sqrt函数
- Win11截图快捷键是什么_Win11自带截图工具
- php订单日志怎么导出excel_php导出订单日
- Win11怎么设置开机自动连接宽带_Windows
- Avalonia如何实现跨窗口通信 Avaloni

QQ客服