[英]How to copy a directory in C++, using std::filesystem::copy?
So, I'm trying to do something that should be simple.所以,我正在尝试做一些应该很简单的事情。 I'm using std::filesystem::copy
to copy one directory into another, as such:我正在使用std::filesystem::copy
将一个目录复制到另一个目录,如下所示:
#include <filesystem>
int main (int argc, char* argv[])
{
const char* dir1 = "C:\\Users\\me\\folder";
const char* dir2 = "C:\\Users\\me\\folder_copy";
std::filesystem::copy(dir1, dir2, std::filesystem::copy_options::update_existing);
return 0;
}
However, the above crashes for me when folder_copy
already exists, with the following error:但是,当folder_copy
已经存在时,上述对我来说会崩溃,并出现以下错误:
Unhandled exception at 0x00007FF932E9A308 in code.exe: Microsoft C++ exception: std::filesystem::filesystem_error at memory location 0x00000012188FF6D0.
Interestingly, the flag std::filesystem::copy_options::overwrite_existing
works fine for me.有趣的是,标志std::filesystem::copy_options::overwrite_existing
对我来说很好用。 Is update_existing
incompatible with std::filesystem::copy
? update_existing
与std::filesystem::copy
不兼容吗? It would be weird if it only worked with std::filesystem::copy_file
.如果它只与std::filesystem::copy_file
一起使用会很奇怪。
Anyway, if this is by design, how do I copy a directory while only updating out-of-date files?无论如何,如果这是设计使然,我如何在仅更新过期文件的同时复制目录?
Try with:尝试:
int main (int argc, char* argv[])
{
namespace fs = std::filesystem;
const char* dir1 = "C:/Users/me/folder";
const char* dir2 = "C:/Users/me/folder_copy";
try {
fs::copy(dir1, dir2,
fs::copy_options::update_existing
//|fs::copy_options::overwrite_existing
|fs::copy_options::recursive);
}
catch (const fs::filesystem_error& e) {
cerr << e.what() << endl;
}
return 0;
}
Also check that no file in the destination folder is in use while being updated and that you have sufficient write permissions.还要检查目标文件夹中的文件在更新时是否正在使用,并且您有足够的写入权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.