文件或目录的复制
std::filesystem::copy 函数默认会在目标位置存在同名文件时抛出异常,而不会覆盖已存在的文件。但是你可以通过传递一个额外的参数来指定复制操作是否覆盖已存在的文件。在C++17及以上版本中,可以使用 std::filesystem::copy_options 枚举来实现这一点。
#include <filesystem>namespace fs = std::filesystem;int main() {fs::path from = "a.txt";fs::path to = "b.txt";// 使用 copy_options::overwrite_existing 选项覆盖已存在的文件fs::copy(from, to, fs::copy_options::overwrite_existing);return 0;
}
在LInux上的编译器完美通过。但在windows下用的MinGW支持C++20的版本,支持其他C++20的特性,但上面代码依然报错文件已存在。