std::experi...system::copy(3)
β¨ NAME β¨
okay so this is the std::experi...system::copy(3) page from the C++ Standard Libary manual, and the functionβs full name is literally std::experimental::filesystem::copy β like, both sides of the dash are std::experimental::filesystem::copy, no cap. She copies files and directories, and sheβs got options for days. π
π¦ Synopsis π¦
Defined in header <experimental/filesystem>, bestie:
Defined in header <experimental/filesystem>
void copy( const path& from, const path& to ); (1)
(filesystem TS)
void copy( const path& from, const path& to, error_code& ec );
void copy( const path& from, const path& to, copy_options options ); (2)
(filesystem TS)
void copy( const path& from, const path& to, copy_options options, error_code& ec );
She copies files and directories, with a whole variety of options:
- The default β equivalent to (2) with
copy_options::noneused asoptions. - Copies the file or directory
fromto file or directoryto, using the copy options indicated byoptions.
Undefined behavior alert, bestie: the behavior is undefined if there is more than one option in any of thecopy_optionsoption group present inoptions(even in thecopy_filegroup, which is not relevant tocopy). Do not test her like that. π
The behavior is as follows:
- First, before doing anything else, obtains type and permissions of
fromby no more than single call tostatus(or, ifcopy_options::skip_symlinksorcopy_options::create_symlinksare present inoptions, by a call tosymlink_status). - If necessary, obtains the status of
tothe same way, by no more than a singlestatusorsymlink_statuscall. - If
fromdoes not exist, reports an error. - If
fromandtoare the same file as determined byequivalent(), reports an error. - If either
fromortois not a regular file, a directory, or a symlink, as determined byis_other, reports an error. - If
fromis a directory, buttois a regular file, reports an error. - If
fromis a symbolic link, then- If
copy_options::skip_symlinkis present inoptions, does nothing. - Otherwise, if
todoes not exist andcopy_options::copy_symlinksis present inoptions, then behaves as ifcopy_symlink(from, to). - Otherwise, reports an error.
- If
- Otherwise, if
fromis a regular file, then- If
copy_options::directories_onlyis present inoptions, does nothing. - Otherwise, if
copy_options::create_symlinksis present inoptions, creates a symlink toto. Note:frommust be an absolute path unlesstois in the current directory. - Otherwise, if
copy_options::create_hard_linksis present inoptions, creates a hard link toto. - Otherwise, if
tois a directory, then behaves as ifcopy_file(from, to/from.filename(), options)(creates a copy offromas a file in the directoryto). - Otherwise, behaves as if
copy_file(from, to, options)(copies the file).
- If
- Otherwise, if
fromis a directory and eitheroptionshascopy_options::recursiveor iscopy_options::none.- If
todoes not exist, first executescreate_directory(to, from)(creates the new directory with a copy of the old directory's attributes). - Then, whether
toalready existed or was just created, iterates over the files contained infromas if byfor (const directory_entry& x : directory_iterator(from))and for each directory entry, recursively callscopy(x.path(), to/x.path().filename(), options | unspecified), whereunspecifiedis a special bit that has no other effect when set inoptions(the sole purpose of setting this bit is to prevent recursive copying subdirectories ifoptionsiscopy_options::none).
- If
- Otherwise does nothing.
π Parameters π
fromβ path to the source file, directory, or symlinktoβ path to the target file, directory, or symlinkecβ out-parameter for error reporting in the non-throwing overload
πΈ Return value πΈ
(none) β literally no return value, bestie. Sheβs doing side effects, not handing you a little return gift. π
π₯ Exceptions π₯
The overload that does not take an error_code& parameter throws filesystem_error on underlying OS API errors, constructed with from 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 an 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 noexcept specification: noexcept.
π¦ Notes π¦
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
// After
std::experimental::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::experimental::filesystem::copy("/dir1", "/dir3", 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
π§ Example π§
// Run this code
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::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)
// sandbox holds 2 files and 2 directories, one of which has a subdirectory
// sandbox/file1.txt
// sandbox/file2.txt
// sandbox/dir2
// sandbox/dir
// sandbox/dir/subdir
fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive);
// sandbox/copy holds copies of the above files and subdirectories
fs::remove_all("sandbox");
}
π See also π
copy_optionsβ specifies semantics of copy operations (enum)copy_symlinkβ copies a symbolic link (function)copy_fileβ copies file contents (function)
π Categories π
- Noindexed pages
- unconditionally noexcept
π Hidden categories π
- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template
mwah π this page came from http://cppreference.com, date/version 2024.06.10, page title std::experi...system::copy(3). bye bestie, go copy something iconic π«Άβ¨
π¦ 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