std::filesystem::path 构造含反斜杠的 Windows 路径时崩溃,因 C++ 字符串字面量中 是转义符,导致路径解析错误;应使用原始字符串 R"(C: empile.txt)" 或正斜杠 "C:/temp/file.txt",运行时路径需确保 JSON 等解析已正确处理双反斜杠,调试时用十六进制 dump 验证真实字节。

为什么 std::filesystem::path 直接构造会崩在带反斜杠的 Windows 路径上
因为 C++ 字符串字面量里 是转义符,不是路径分隔符。你写 "C:Users
amedoc",编译器实际看到的是 C:sers ameoc——U、
、d 全被当成了转义序列,轻则路径错乱,重则编译失败或运行时 std::filesystem::path 构造失败(抛 std::filesystem::filesystem_error)。
常见错误现象:std::filesystem::path p("C: emplog.txt"); 编译不报错但运行时报 “No such file or directory”,实际是因为路径根本没解析对。
- 用原始字符串字面量:改写成
R"(C: emplog.txt)",不再被解释,原样保留 - Windows 下推荐统一用正斜杠:
"C:/temp/log.txt",std::filesystem::path内部自动兼容,且避免所有转义问题 - 如果必须拼接路径(比如变量 + 固定后缀),别用
+拼 raw string,而要用path的/重载:std::filesystem::path base = R"(C:data)"; auto full = base / "config.json";