| Classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| File types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <filesystem> | ||
void copy(conststd::filesystem::path& from, conststd::filesystem::path& to); | (1) | (since C++17) |
void copy(conststd::filesystem::path& from, conststd::filesystem::path& to, | (2) | (since C++17) |
void copy(conststd::filesystem::path& from, conststd::filesystem::path& to, | (3) | (since C++17) |
void copy(conststd::filesystem::path& from, conststd::filesystem::path& to, | (4) | (since C++17) |
Copies files and directories, with a variety of options.
copy_options::none used asoptions.copy_file group).The behavior is as follows:
copy_options::skip_symlinks,copy_options::copy_symlinks, orcopy_options::create_symlinks is present inoptions;copy_options::skip_symlinks orcopy_options::create_symlinks is present inoptions;copy_options::copy_symlinks is present inoptions).copy_options::skip_symlink is present inoptions, does nothing.copy_options::copy_symlinks is present inoptions, then behaves as ifcopy_symlink(from, to).copy_options::directories_only is present inoptions, does nothing.copy_options::create_symlinks is present inoptions, creates a symlink toto. Note:from must be an absolute path unlessto is in the current directory.copy_options::create_hard_links is present inoptions, creates a hard link toto.copy_options::create_symlinks is set inoptions, reports an error with an error code equal tostd::make_error_code(std::errc::is_a_directory).copy_options::recursive or iscopy_options::none,copy_options::none.)Contents |
| from | - | path to the source file, directory, or symlink |
| to | - | path to the target file, directory, or symlink |
| ec | - | out-parameter for error reporting in the non-throwing overload |
(none)
Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.
The 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// Afterstd::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 withcopy_options::recursive, the subdirectories are also copied, with their content, recursively.
// ...but afterstd::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
#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)constauto 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
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++17 | error_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 |
(C++17) | specifies semantics of copy operations (enum)[edit] |
(C++17) | copies a symbolic link (function)[edit] |
(C++17) | copies file contents (function)[edit] |