std :: filesystem ::复制文件将文件复制到另一个预先存在的目录的错误

发布于 2025-01-19 04:16:52 字数 778 浏览 2 评论 0原文

请参阅下面的以下代码,以及下面的错误。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半步萧音过轻尘 2025-01-26 04:16:52

std::filesystem::copy 有记录。您正在使用第一个重载,但需要第二个:

  1. void copy(from, to),这相当于使用copy_options::none的[重载2,如下]
  2. < code>void copy(from, to, options)

在调用 copy 之前编写语句 std::filesystem::copy_options::update_existing; 根本没有实现任何目标,而传递正确重载的选项,例如

std::filesystem::copy(sourcepath, destpath,
                      std::filesystem::copy_options::update_existing);

应该做你想做的事。

...它可以在我的家用计算机上使用 Visual Studio 2022 运行...

您没有说明在这种情况下目标文件是否存在,这是您应该检查的第一事情。

我将 copy_options 放在复制函数中,但它不起作用,所以我开始移动它,我可能应该提到这一点。

随机排列代码并不是生成干净示例供其他人帮助的好方法。

在极少数情况下,破解某些东西确实可以解决问题,我强烈建议停下来找出原因。当你修改了一些东西但它仍然不起作用时,请务必留下注释来提醒自己你尝试过什么,但代码本身应该仍然处于良好状态。

当我编写 std::filesystem::copy(sourcepath, destpath, std::filesystem::copy_options::recursive)

时仍然不起作用

嗯,这是一个不同的选项,不是吗?您是否也随机排列了您选择的 copy_options

尝试递归和 update_existing 会产生相同的问题。

文档说

如果 options 中的任一 copy_options 选项组中有多个选项(即使在 copy_file 组中),则该行为未定义。

所以无论如何你都不应该同时设置两者。递归复制单个文件没有任何好处,但更新或覆盖文件可能有好处。如果目的地已经存在。根据您的错误,确实如此。

由于您确实有一个明确指出“文件存在”的错误,因此您当然应该查看“当文件已存在时控制copy_file()的选项”表的部分此处

The overloads of std::filesystem::copy are documented. You're using the first overload, but want the second:

  1. void copy(from, to) which is equivalent to [overload 2, below] using copy_options::none
  2. 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 like

std::filesystem::copy(sourcepath, destpath,
                      std::filesystem::copy_options::update_existing);

should do what you want.

... it worked on my home computer using visual studio 2022 ...

you don't say whether the destination file existed in that case, which is the first thing you should check.

I put the copy_options within the copy function but it didn't work so I started moving it around, I probably should have mentioned that.

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.

Still doesn't work when I write std::filesystem::copy(sourcepath, destpath, std::filesystem::copy_options::recursive)

Well, that's a different option, isn't it? Were you randomly permuting which copy_options you selected as well?

Trying recursive and update_existing yields the same issue.

The documentation says

The behavior is undefined if there is more than one option in any of the copy_options option group present in options (even in the copy_file group).

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.

为人所爱 2025-01-26 04:16:52

使用:
filesystem::copy_file(oldPath, newPath, filesystem::copy_options::overwrite_existing);

Using:
filesystem::copy_file(oldPath, newPath, filesystem::copy_options::overwrite_existing);

梦里南柯 2025-01-26 04:16:52

Visual Studio 2022解决了问题

Visual Studio 2022 fixed the problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文