C++ 如何使用覆盖执行 boost::filesystem copy_file

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/829468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:29:25  来源:igfitidea点击:

how to perform boost::filesystem copy_file with overwrite

c++windowsboostboost-filesystem

提问by Dani van der Meer

The Windows API function CopyFilehas an argument BOOL bFailIfExiststhat allows you to control whether or not you want to overwrite the target file if it exists.

Windows API 函数CopyFile有一个参数BOOL bFailIfExists,允许您控制是否要覆盖目标文件(如果存在)。

The boost::filesystemcopy_filefunction has no such argument, and will fail if the target file exists. Is there an elegant way to use the boost copy_file function and overwrite the target file? Or is it better to simply use the Windows API? My current target platform is Windows, but I prefer to use STL and boost where possible to keep my code platform independent.

boost::filesystemcopy_file函数没有这样的参数,如果目标文件存在就会失败。有没有一种优雅的方法来使用 boost copy_file 函数并覆盖目标文件?还是简单地使用 Windows API 更好?我当前的目标平台是 Windows,但我更喜欢在可能的情况下使用 STL 和 boost 以保持我的代码平台独立。

Thank you.

谢谢你。

回答by anno

There's a third enumargument to copy_file, boost::filesystem::copy_option::overwrite_if_exists

copy_file有第三个枚举参数,boost::filesystem::copy_option::overwrite_if_exists

copy_file(source_path,destination_path,copy_option::overwrite_if_exists);

回答by Vitaly P

Beware of boost::copy_file with copy_option::overwrite_if_exists! If the destination file exists and it is smaller than the source, the function will only overwrite the first size(from_file) bytes in the target file.

当心带有 copy_option::overwrite_if_exists 的 boost::copy_file!如果目标文件存在且小于源文件,则该函数将仅覆盖目标文件中的第一个 size(from_file) 字节。

At least for me this was a caveat since I presumed copy_option::overwrite_if_exists affects filesand not content

至少对我来说这是一个警告,因为我认为 copy_option::overwrite_if_exists 影响文件而不是内容

回答by jon-hanson

Test if the destination file exists first and if it does then remove it :

首先测试目标文件是否存在,如果存在则将其删除:

if (exists (to_fp))
    remove (to_fp);
copy_file (from_fp, to_fp);

Or if you're worried about the file appearing between the test and the copy then you could write to a temporary file and then rename it to the destination file.

或者,如果您担心出现在测试和副本之间的文件,那么您可以写入一个临时文件,然后将其重命名为目标文件。

回答by dirkgently

Is there an elegant way to use the boost copy_file function and overwrite the target file?

有没有一种优雅的方法来使用 boost copy_file 函数并覆盖目标文件?

Apparently there's no direct API to do this.

显然没有直接的 API 来做到这一点。

Or is it better to simply use the Windows API? My current target platform is Windows, but I prefer to use STL and boost where possible to keep my code platform independent.

还是简单地使用 Windows API 更好?我当前的目标平台是 Windows,但我更喜欢在可能的情况下使用 STL 和 boost 以保持我的代码平台独立。

From the documentation:

从文档:

A proposal, N1975, to include Boost.Filesystem in Technical Report 2 has been accepted by the C++ Standards Committee. The Boost.Filesystem library will stay in alignment with the TR2 Filesystem proposal as it works its way through the TR2 process. Note, however, that namespaces and header granularity differs between Boost.Filesystem and the TR2 proposal.

将 Boost.Filesystem 包含在技术报告 2 中的提案 N1975 已被 C++ 标准委员会接受。Boost.Filesystem 库将与 TR2 文件系统提案保持一致,因为它通过 TR2 流程工作。但是请注意,Boost.Filesystem 和 TR2 提案之间的命名空间和标头粒度是不同的。

Which strongly suggests that sticking with boost::filesystemis a good idea.

这强烈表明坚持boost::filesystem是一个好主意。