Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::filesystem::copy_file

      From cppreference.com
      <cpp‎ |experimental‎ |fs
       
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<experimental/filesystem>
      bool copy_file(const path& from,const path& to);
      bool copy_file(const path& from,const path& to, error_code& ec);
      (1)(filesystem TS)
      bool copy_file(const path& from,const path& to, copy_options options);
      bool copy_file(const path& from,const path& to, copy_options options, error_code& ec);
      (2)(filesystem TS)
      1) The default, equivalent to(2) withcopy_options::none used asoptions.
      2) 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 tocopy_file).
      • 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:
      • Ifto andfrom are the same as determined byequivalent(from, to), report an error.
      • Otherwise, if none of the copy_file control options are set inoptions, report an error.
      • 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 bylast_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

      The overload that does not take anerror_code& parameter throwsfilesystem_error on underlying OS API errors, constructed withfrom 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 anerror_code& parameter sets it to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur. This overload has
      noexcept specification:  
      noexcept
        

      [edit]Notes

      The functions involve at most one direct or indirect call tostatus(to) (used both to determine if the file exists, and, forcopy_options::update_existing option, its last write time).

      Error is reported whencopy_file is used to copy a directory: usecopy for that.

      copy_file follows symlinks: usecopy_symlink orcopy withcopy_options::copy_symlinks for that.

      [edit]Example

      Run this code
      #include <experimental/filesystem>#include <fstream>#include <iostream>namespace fs= std::experimental::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]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp