std :: filesystem ::复制文件将文件复制到另一个预先存在的目录的错误
请参阅下面的以下代码,以及下面的错误。
std::string source = "C:\\Users\\cambarchian\\Documents\\tested";
std::string destination = "C:\\Users\\cambarchian\\Documents\\tester";
std::filesystem::path sourcepath = source;
std::filesystem::path destpath = destination;
std::filesystem::copy_options::update_existing;
std::filesystem::copy(sourcepath, destpath);
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot copy: File exists [C:\Users\cambarchian\Documents\tested] [C:\Users\cambarchian\Documents\tester]
尝试使用 filesystem::copy 并尝试不同的路径。什么事都没有运气。我在这里不能写太多,因为上面列出了问题,可能是一个简单的格式问题。话虽这么说,它可以在我的家用计算机上使用 Visual Studio 2022 运行,但是使用 VS Code 和 gcc 11.2 给我带来了这个问题。
See below for the following code, and below that, the error that follows.
std::string source = "C:\\Users\\cambarchian\\Documents\\tested";
std::string destination = "C:\\Users\\cambarchian\\Documents\\tester";
std::filesystem::path sourcepath = source;
std::filesystem::path destpath = destination;
std::filesystem::copy_options::update_existing;
std::filesystem::copy(sourcepath, destpath);
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot copy: File exists [C:\Users\cambarchian\Documents\tested] [C:\Users\cambarchian\Documents\tester]
Tried to use filesystem::copy, along with trying different paths. No luck with anything. Not too much I can write here as the problem is listed above, could be a simple formatting issue. That being said, it worked on my home computer using visual studio 2022, however using VS Code with gcc 11.2 gives me this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
std::filesystem::copy
有记录。您正在使用第一个重载,但需要第二个:void copy(from, to)
,这相当于使用copy_options::none
的[重载2,如下]在调用 copy 之前编写语句
std::filesystem::copy_options::update_existing;
根本没有实现任何目标,而传递正确重载的选项,例如应该做你想做的事。
您没有说明在这种情况下目标文件是否存在,这是您应该检查的第一事情。
随机排列代码并不是生成干净示例供其他人帮助的好方法。
在极少数情况下,破解某些东西确实可以解决问题,我强烈建议停下来找出原因。当你修改了一些东西但它仍然不起作用时,请务必留下注释来提醒自己你尝试过什么,但代码本身应该仍然处于良好状态。
嗯,这是一个不同的选项,不是吗?您是否也随机排列了您选择的
copy_options
?文档说
所以无论如何你都不应该同时设置两者。递归复制单个文件没有任何好处,但更新或覆盖文件可能有好处。如果目的地已经存在。根据您的错误,确实如此。
由于您确实有一个明确指出“文件存在”的错误,因此您当然应该查看“当文件已存在时控制
copy_file()
的选项”表的部分此处。The overloads of
std::filesystem::copy
are documented. You're using the first overload, but want the second:void copy(from, to)
which is equivalent to [overload 2, below] usingcopy_options::none
void copy(from, to, options)
Writing the statement
std::filesystem::copy_options::update_existing;
before calling copy doesn't achieve anything at all, whereas passing the option to the correct overload likeshould do what you want.
you don't say whether the destination file existed in that case, which is the first thing you should check.
Randomly permuting your code isn't a good way of generating clean examples for others to help with.
In the rare event that hacking away at something does fix it, I strongly recommend pausing to figure out why. When you've hacked away at something and it still doesn't work, by all means leave comments to remind yourself what you tried, but the code itself should still be in a good state.
Well, that's a different option, isn't it? Were you randomly permuting which
copy_options
you selected as well?The documentation says
so you shouldn't be setting both anyway. There's no benefit to recursively copying a single file, but there may be a benefit to updating or overwriting one. If the destination already exists. Which, according to your error, it does.
Since you do have an error explicitly saying "File exists", you should certainly look at the "options controlling
copy_file()
when the file already exists" section of the table here.使用:
filesystem::copy_file(oldPath, newPath, filesystem::copy_options::overwrite_existing);
Using:
filesystem::copy_file(oldPath, newPath, filesystem::copy_options::overwrite_existing);
Visual Studio 2022解决了问题
Visual Studio 2022 fixed the problem