| Technical Specification | ||||
| Filesystem library(filesystem TS) | ||||
| Library fundamentals(library fundamentals TS) | ||||
| Library fundamentals 2(library fundamentals TS v2) | ||||
| Library fundamentals 3(library fundamentals TS v3) | ||||
| Extensions for parallelism(parallelism TS) | ||||
| Extensions for parallelism 2(parallelism TS v2) | ||||
| Extensions for concurrency(concurrency TS) | ||||
| Extensions for concurrency 2(concurrency TS v2) | ||||
| Concepts(concepts TS) | ||||
| Ranges(ranges TS) | ||||
| Reflection(reflection TS) | ||||
| Mathematical special functions(special functions TR) | ||||
| Experimental Non-TS | ||||
| Pattern Matching | ||||
| Linear Algebra | ||||
| std::execution | ||||
| Contracts | ||||
| 2D Graphics |
| 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) |
copy_options::none used asoptions.copy_file).copy_options::skip_existing is set inoptions, do nothing.copy_options::overwrite_existing is set inoptions, copy the contents and the attributes of the file to whichfrom resolves to the file to whichto resolves.copy_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 |
| from | - | path to the source file |
| to | - | path to the target file |
| ec | - | out-parameter for error reporting in the non-throwing overload |
true if the file was copied,false otherwise.
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.
#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"
| specifies semantics of copy operations (enum)[edit] | |
| copies a symbolic link (function)[edit] | |
| copies files or directories (function)[edit] |