Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::copy_file

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      bool copy_file(conststd::filesystem::path& from,
                     conststd::filesystem::path& to);
      (1)(since C++17)
      bool copy_file(conststd::filesystem::path& from,

                     conststd::filesystem::path& to,

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

                     conststd::filesystem::path& to,

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

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

                     std::error_code& ec);
      (4)(since C++17)
      1,2) The default, equivalent to(3,4) withcopy_options::none used asoptions.
      3,4) Copies a single file fromfrom toto, 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 the groups not relevant tofilesystem::copy_file).
      • If!filesystem::is_regular_file(from) (either because the source file doesn't exist or because it is not a regular file), report an error.
      • Otherwise, if the destination file does not exist,
      • copies the contents and the attributes of the file to whichfrom resolves to the file to whichto resolves (symlinks are followed).
      • Otherwise, if the destination file already exists,
      • report an error if any of the following is true:
      • Otherwise, ifcopy_options::skip_existing is set inoptions, do nothing.
      • Otherwise, ifcopy_options::overwrite_existing is set inoptions, copy the contents and the attributes of the file to whichfrom resolves to the file to whichto resolves.
      • Otherwise, ifcopy_options::update_existing is set inoptions, only copy the file iffrom is newer thanto, as defined byfilesystem::last_write_time().

      The non-throwing overloads returnfalse if an error occurs.

      Contents

      [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 value

      true if the file was copied,false otherwise.

      [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 functions involve at most one direct or indirect call tofilesystem::status(to) (used both to determine if the file exists, and, forfilesystem::copy_options::update_existing option, its last write time).

      Error is reported whenfilesystem::copy_file is used to copy a directory: usefilesystem::copy for that.

      filesystem::copy_file follows symlinks: usefilesystem::copy_symlink orfilesystem::copy withfilesystem::copy_options::copy_symlinks for that.

      [edit]Example

      Run this code
      #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: afile2.txt holds: aCould 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.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3014C++17error_code overload marked noexcept but can allocate memorynoexcept removed

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp