std::filesystem::copy_file(3)
πΈ NAME πΈ
okay bestie, welcome to std::filesystem::copy_file(3) β the C++ Standard Libary page for std::filesystem::copy_file (yes, thatβs the spelling in the header, not me π). Itβs giving βcopy one file, but make it overloaded,β and weβre gonna get into every single detail π
β¨.
π¦ Synopsis π¦
Defined in header <filesystem>.
bool copy_file( const std::filesystem::path& from, (1) (since C++17)
const std::filesystem::path& to );
bool copy_file( const std::filesystem::path& from,
const std::filesystem::path& to, (2) (since C++17)
std::error_code& ec );
bool copy_file( const std::filesystem::path& from,
const std::filesystem::path& to, (3) (since C++17)
std::filesystem::copy_options options );
bool copy_file( const std::filesystem::path& from,
const std::filesystem::path& to, (4) (since C++17)
std::filesystem::copy_options options,
std::error_code& ec );
- 1,2) The default, equivalent to (3,4) with
copy_options::noneused asoptions. - 3,4) Copies a single file from
fromtoto, using the copy options indicated byoptions. The behavior is undefined if there is more than one option in any of thecopy_optionsoption group present inoptions(even in the groups not relevant tofilesystem::copy_file) π.- If
!filesystem::is_regular_file(from)(either because the source file doesn't exist or because it is not a regular file), report an error. - Otherwise, if the destination file does not exist,
- copies the contents and the attributes of the file to which
fromresolves to the file to whichtoresolves (symlinks are followed).
- copies the contents and the attributes of the file to which
- Otherwise, if the destination file already exists,
- report an error if any of the following is true:
toandfromare the same as determined byfilesystem::equivalent(from, to);tois not a regular file as determined by!filesystem::is_regular_file(to);- none of the
filesystem::copy_filecontrol options are set inoptions.
- Otherwise, if
copy_options::skip_existingis set inoptions, do nothing. - Otherwise, if
copy_options::overwrite_existingis set inoptions, copy the contents and the attributes of the file to whichfromresolves to the file to whichtoresolves. - Otherwise, if
copy_options::update_existingis set inoptions, only copy the file iffromis newer thanto, as defined byfilesystem::last_write_time().
- report an error if any of the following is true:
- If
The non-throwing overloads return false if an error occurs.
π Parameters π
fromβ path to the source file πtoβ path to the target file π―ecβ out-parameter for error reporting in the non-throwing overload π«Ά
β Return value β
true if the file was copied, false otherwise. simple as that, bestie β¨
π Exceptions π
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.
- 1,3) Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withfromas the first path argument,toas the second path argument, and the OS error code as the error code argument. - 2,4) Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
πΈ Notes πΈ
The functions involve at most one direct or indirect call to filesystem::status(to) (used both to determine if the file exists, and, for filesystem::copy_options::update_existing option, its last write time).
Error is reported when filesystem::copy_file is used to copy a directory: use filesystem::copy for that.
filesystem::copy_file follows symlinks: use filesystem::copy_symlink or filesystem::copy with filesystem::copy_options::copy_symlinks for that.
iykyk: this function is for one file, not a whole directory situation. The page said it, Iβm just saying it with love π.
π§ Example π§
// Run this code
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::create_directory("sandbox");
std::ofstream("sandbox/file1.txt").put('a');
fs::copy_file("sandbox/file1.txt", "sandbox/file2.txt");
// now there are two files in sandbox:
std::cout << "file1.txt holds: "
<< std::ifstream("sandbox/file1.txt").rdbuf() << '\n';
std::cout << "file2.txt holds: "
<< std::ifstream("sandbox/file2.txt").rdbuf() << '\n';
// fail to copy directory
fs::create_directory("sandbox/abc");
try
{
fs::copy_file("sandbox/abc", "sandbox/def");
}
catch (fs::filesystem_error& e)
{
std::cout << "Could not copy sandbox/abc: " << e.what() << '\n';
}
fs::remove_all("sandbox");
}
π Possible output π
file1.txt holds: a
file2.txt holds: a
Could not copy sandbox/abc: copy_file: Is a directory: "sandbox/abc", "sandbox/def"
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 3014β Applied to:C++17β Behavior as published:error_codeoverload markednoexceptbut can allocate memory β Correct behavior:noexceptremoved β
π See also π
copy_optionsβ specifies semantics of copy operations (C++17) (enum)copy_symlinkβ copies a symbolic link (C++17) (function)copyβ copies files or directories (C++17) (function)
xoxo, bestie π this page came from http://cppreference.com, dated 2024.06.10, and the manual name is std::filesystem::copy_file(3) β¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms