Copies files and directories, with a variety of options.
1,2) The default, equivalent to (3,4) with copy_options::none
used as options.
Copies the file or directory
fromto file or directory
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
copy_file
group).
The behavior is as follows:
copy_options::skip_symlinks
, copy_options::copy_symlinks
, or copy_options::create_symlinks
is present in options;copy_options::skip_symlinks
or copy_options::create_symlinks
is present in options;copy_options::copy_symlinks
is present in options).copy_options::skip_symlink
is present in options, does nothing.copy_options::copy_symlinks
is present in options, then behaves as if copy_symlink(from, to).copy_options::directories_only
is present in options, does nothing.copy_options::create_symlinks
is present in options, creates a symlink to to. Note: from must be an absolute path unless to is in the current directory.copy_options::create_hard_links
is present in options, creates a hard link to to.copy_options::create_symlinks
is set in options, reports an error with an error code equal to std::make_error_code(std::errc::is_a_directory).copy_options::recursive
or is copy_options::none
,copy_options::none
.)(none)
[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 default behavior when copying directories is the non-recursive copy: the files are copied, but not the subdirectories:
// Given // /dir1 contains /dir1/file1, /dir1/file2, /dir1/dir2 // and /dir1/dir2 contains /dir1/dir2/file3 // After std::filesystem::copy("/dir1", "/dir3"); // /dir3 is created (with the attributes of /dir1) // /dir1/file1 is copied to /dir3/file1 // /dir1/file2 is copied to /dir3/file2
While with copy_options::recursive
, the subdirectories are also copied, with their content, recursively.
// ...but after std::filesystem::copy("/dir1", "/dir3", std::filesystem::copy_options::recursive); // /dir3 is created (with the attributes of /dir1) // /dir1/file1 is copied to /dir3/file1 // /dir1/file2 is copied to /dir3/file2 // /dir3/dir2 is created (with the attributes of /dir1/dir2) // /dir1/dir2/file3 is copied to /dir3/dir2/file3[edit] Example
#include <cstdlib> #include <filesystem> #include <fstream> #include <iostream> namespace fs = std::filesystem; int main() { fs::create_directories("sandbox/dir/subdir"); std::ofstream("sandbox/file1.txt").put('a'); fs::copy("sandbox/file1.txt", "sandbox/file2.txt"); // copy file fs::copy("sandbox/dir", "sandbox/dir2"); // copy directory (non-recursive) const auto copyOptions = fs::copy_options::update_existing | fs::copy_options::recursive | fs::copy_options::directories_only ; fs::copy("sandbox", "sandbox_copy", copyOptions); static_cast<void>(std::system("tree")); fs::remove_all("sandbox"); fs::remove_all("sandbox_copy"); }
Possible output:
. âââ sandbox â âââ dir â â âââ subdir â âââ dir2 â âââ file1.txt â âââ file2.txt âââ sandbox_copy âââ dir â âââ subdir âââ dir2 8 directories, 2 files[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 3013 C++17error_code
overload marked noexcept but can allocate memory noexcept removed LWG 2682 C++17 attempting to create a symlink for a directory succeeds but does nothing reports an error [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