提问者:小点点

std::filesystem::copy抛出filesystem_error


正在尝试运行此代码:

filesystem::copy_options copyOptions = filesystem::copy_options::skip_existing | filesystem::copy_options::recursive | filesystem::copy_options::directories_only;
filesystem::copy(pathA, pathB, copyOptions);

第一次尝试是成功的,操作完全按照我的要求和预期。。。 第二次尝试(在创建pathB结构之后)失败,出现以下错误:

filesystem error: cannot copy: File exists 
[C:\Users\Smith\Documents\Projects\ProjectA\bin\..\pathA] 
[C:\Users\Smith\Documents\Projects\ProjectA\bin\..\pathB]

我希望使用skip_existing或overwrite_existing不会引发此错误。 如何使用这种复制方法而不必每次使用前都删除pathB?

链接到我正在查看的cppreference


共1个答案

匿名用户

您需要使用标志std::filesystem::copy_options::overwrite_existing

这解决了你的问题。