std::filesystem::copy_file
| Defined in header <filesystem>
|
||
| bool copy_file( const std::filesystem::path& from, const std::filesystem::path& to ); |
(1) | (since C++17) |
| bool copy_file( const std::filesystem::path& from, const std::filesystem::path& to, |
(2) | (since C++17) |
copy_options::none used as optionsfrom to to, using the copy options indicated by options. 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 groups not relevant to copy_file)
- If !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 by equivalent(from, to); - if
tois not a regular file as determined by !is_regular_file(to) - if none of the {{tt|copy_file{{control options are set in
options
-
- 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 by last_write_time()
The non-throwing overloads return false if an error occurs.
Contents |
[edit] Parameters
| from | - | path to the source file |
| to | - | path to the target file |
| ec | - | out-parameter for error reporting in the non-throwing overload |
[edit] Return value
true if the file was copied, false otherwise.
[edit] Exceptions
The overload that does not take a std::error_code& parameter throws filesystem_error on underlying OS API errors, constructed withfrom as the first argument, to as the second argument, and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails. The overload taking a std::error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. This overload has [edit] Notes
The functions involve at most one direct or indirect call to status(to) (used both to determine if the file exists, and, for copy_options::update_existing option, its last write time)
Error is reported when copy_file is used to copy a directory: use copy for that.
copy_file follows symlinks: use copy_symlink or copy with copy_options::copy_symlinks for that.
[edit] Examples
#include <fstream> #include <iostream> #include <filesystem> 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"
[edit] See also
| (C++17) |
specifies semantics of copy operations (enum) |
| (C++17) |
copies a symbolic link (function) |
| (C++17) |
copies files or directories (function) |