1,2) The default, equivalent to (3,4) with copy_options::none
used as options.
Copies a single file from
fromto
to, using the copy options indicated by
options. The behavior is undefined if there is more than one option in any of the
copy_optionsoption group present in
options(even in the groups not relevant to
filesystem::copy_file).
copy_options::skip_existing
is set in options, do nothing.copy_options::overwrite_existing
is set in options, copy the contents and the attributes of the file to which from resolves to the file to which to resolves.copy_options::update_existing
is set in options, only copy the file if from is newer than to, as defined by filesystem::last_write_time().The non-throwing overloads return false if an error occurs.
[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 valuetrue if the file was copied, false otherwise.
[edit] ExceptionsAny overload not marked noexcept
may throw std::bad_alloc if memory allocation fails.
Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed with
fromas 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 executes
ec.clear()if no errors occur.
[edit] NotesThe 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.
#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"[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 3014 C++17error_code
overload marked noexcept but can allocate memory noexcept removed [edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4