Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::copy

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      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,

                 std::error_code& ec);
      (2)(since C++17)
      void copy(conststd::filesystem::path& from,

                 conststd::filesystem::path& to,

                 std::filesystem::copy_options options);
      (3)(since C++17)
      void copy(conststd::filesystem::path& from,

                 conststd::filesystem::path& to,
                 std::filesystem::copy_options options,

                 std::error_code& ec);
      (4)(since C++17)

      Copies files and directories, with a variety of options.

      1,2) The default, equivalent to(3,4) withcopy_options::none used asoptions.
      3,4) Copies the file or directoryfrom to file or directoryto, using the copy options indicated byoptions. The behavior is undefined if there is more than one option in any of thecopy_options option group present inoptions (even in thecopy_file group).

      The behavior is as follows:

      • First, before doing anything else, obtains type and permissions offrom by no more than a single call to
      • If necessary, obtains the status ofto, by no more than a single call to
      • If eitherfrom orto has an implementation-definedfile type, the effects of this function are implementation-defined.
      • Iffrom does not exist, reports an error.
      • Iffrom andto are the same file as determined bystd::filesystem::equivalent, reports an error.
      • If eitherfrom orto is not a regular file, a directory, or a symlink, as determined bystd::filesystem::is_other, reports an error.
      • Iffrom is a directory, butto is a regular file, reports an error.
      • Iffrom is a symbolic link, then
      • Ifcopy_options::skip_symlink is present inoptions, does nothing.
      • Otherwise, ifto does not exist andcopy_options::copy_symlinks is present inoptions, then behaves as ifcopy_symlink(from, to).
      • Otherwise, reports an error.
      • Otherwise, iffrom is a regular file, then
      • Ifcopy_options::directories_only is present inoptions, does nothing.
      • Otherwise, ifcopy_options::create_symlinks is present inoptions, creates a symlink toto. Note:from must be an absolute path unlessto is in the current directory.
      • Otherwise, ifcopy_options::create_hard_links is present inoptions, creates a hard link toto.
      • Otherwise, ifto is a directory, then behaves as ifcopy_file(from, to/from.filename(), options) (creates a copy offrom as a file in the directoryto).
      • Otherwise, behaves as ifcopy_file(from, to, options) (copies the file).
      • Otherwise, iffrom is a directory andcopy_options::create_symlinks is set inoptions, reports an error with an error code equal tostd::make_error_code(std::errc::is_a_directory).
      • Otherwise, iffrom is a directory and eitheroptions hascopy_options::recursive or iscopy_options::none,
      • Ifto does not exist, first executescreate_directory(to, from) (creates the new directory with a copy of the old directory's attributes).
      • Then, whetherto already existed or was just created, iterates over the files contained infrom as if byfor(conststd::filesystem::directory_entry& x:std::filesystem::directory_iterator(from)) and for each directory entry, recursively callscopy(x.path(), to/x.path().filename(), options| in-recursive-copy), wherein-recursive-copy is a special bit that has no other effect when set inoptions. (The sole purpose of setting this bit is to prevent recursive copying subdirectories ifoptions iscopy_options::none.)
      • Otherwise does nothing.

      Contents

      [edit]Parameters

      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

      [edit]Return value

      (none)

      [edit]Exceptions

      Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.

      1,3) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withfrom as the first path argument,to as the second path argument, and the OS error code as the error code argument.
      2,4) Sets astd::error_code& parameter to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur.

      [edit]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// 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

      [edit]Example

      Run this code
      #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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3013C++17error_code overload marked noexcept but can allocate memorynoexcept removed
      LWG 2682C++17attempting to create a symlink for a directory succeeds but does nothingreports an error

      [edit]See also

      specifies semantics of copy operations
      (enum)[edit]
      copies a symbolic link
      (function)[edit]
      (C++17)
      copies file contents
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/copy&oldid=157937"

      [8]ページ先頭

      ©2009-2025 Movatter.jp