how to perform boost::filesystem copy_file with overwrite

C++WindowsBoostBoost Filesystem

C++ Problem Overview


The Windows API function CopyFile has an argument BOOL bFailIfExists that allows you to control whether or not you want to overwrite the target file if it exists.

The boost::filesystem copy_file function 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.

Thank you.

C++ Solutions


Solution 1 - C++

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

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

https://www.boost.org/doc/libs/1_75_0/libs/filesystem/doc/reference.html

Solution 2 - C++

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.

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

Solution 3 - C++

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.

Solution 4 - C++

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

Apparently there's no direct API to do this.

> 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.

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.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDani van der MeerView Question on Stackoverflow
Solution 1 - C++annoView Answer on Stackoverflow
Solution 2 - C++Vitaly PView Answer on Stackoverflow
Solution 3 - C++jon-hansonView Answer on Stackoverflow
Solution 4 - C++dirkgentlyView Answer on Stackoverflow