可以,但需注意它不递归复制目录且不自动创建父目录;单文件拷贝需显式调用create_directories()并指定overwrite_existing选项。
可以,但要注意它默认不递归复制目录,也不自动创建缺失的父目录。它的行为更接近底层文件操作原语,不是 cp -r 的完整等价物。
std::filesystem::filesystem_error)copy_options::recursive
create_directories() 创建必须处理异常,并显式指定覆盖策略。以下代码能覆盖目标文件,且自动创建目标所在目录:
try {
std::filesystem::create_directories(std::filesystem::path("dst/").parent_path());
std::filesystem::copy("src.txt", "dst.txt",
std::filesystem::copy_options::overwrite_existing);
} catch (const std::filesystem::filesystem_error& e) {
// 例如:权限不足、路径太长、设备满等
std::cerr << "copy failed: " << e.what() << "\n";
}
create_directories() 只创建末级目录及其所有缺失的祖先目录overwrite_existing 是关键,否则目标存在时抛出异常exists("dst.txt") 预判再决定是否删旧文件,但有竞态风险递归复制不是默认行为,漏掉 copy_options::recursive 会导致只拷贝空目录。
copy_options::recursive | copy_options::overwrite_existing
recursive 会尝试合并内容(类似 rsync -r),不是“清空再覆写”
copy_options::copy_symlinks
示例:
std::filesystem::copy("src_dir", "dst_dir",
std::filesystem::copy_options::recursive |
std::filesystem::copy_options::overwrite_existing);
Windows 上反斜杠 \ 在 C++ 字符串中需转义,建议统一用正斜杠或 std::filesystem::path 构造:
"C:\data\file.txt" → \d 和 \f 是非法转义R"(C:\data\file.txt)" 或 "C:/data/file.txt" 或 std::filesystem::path("C:") / "data" / "file.txt"
copy 失败(错误码常为 ACCESS_DENIED)std::filesystem 原生支持 UTF-8 / UTF-16)真正容易被忽略的是:跨分区复制大文件时,std::filesystem::copy 内部仍是“读+写”而非系统级的快速移动(如 Windows 的 CopyFileEx with COPY_FILE_COPY_SYMLINK)。性能敏感场景应考虑调用平台 API 或分块流式拷贝。