命名空间
变体
操作

std::experimental::filesystem::copy

来自 cppreference.cn
< cpp‎ | experimental‎ | fs
 
 
实验性
技术规范
文件系统库 (文件系统 TS)
库基础 (库基础 TS)
库基础 2 (库基础 TS v2)
库基础 3 (库基础 TS v3)
并行性扩展 (并行性 TS)
并行性扩展 2 (并行性 TS v2)
并发性扩展 (并发性 TS)
并发扩展 2 (concurrency TS v2)
概念 (概念 TS)
范围 (范围 TS)
反射 (反射 TS)
数学特殊函数 (特殊函数 TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
定义于头文件 <experimental/filesystem>
void copy( const path& from, const path& to );
void copy( const path& from, const path& to, error_code& ec );
(1) (filesystem TS)
void copy( const path& from, const path& to, copy_options options );
void copy( const path& from, const path& to, copy_options options, error_code& ec );
(2) (filesystem TS)

拷贝文件和目录,提供多种选项

1) 默认行为,等价于使用 copy_options::none 作为 options(2)
2) 将文件或目录 from 拷贝到文件或目录 to,使用 options 指定的拷贝选项。如果 options 中存在 copy_options 选项组中的多个选项(即使是与 copy 无关的 copy_file 组中的选项),则行为未定义。

行为如下

  • 首先,在执行任何其他操作之前,通过不多于一次对 status 的调用(或者,如果 options 中存在 copy_options::skip_symlinkscopy_options::create_symlinks,则通过对 symlink_status 的调用)获取 from 的类型和权限。
  • 如有必要,以相同方式通过不多于一次 status 或 symlink_status 调用获取 to 的状态。
  • 如果 from 不存在,则报告错误。
  • 如果 fromto 是通过 equivalent() 确定的同一文件,则报告错误。
  • 如果 fromto 不是常规文件、目录或符号链接(由 is_other 确定),则报告错误。
  • 如果 from 是目录,但 to 是常规文件,则报告错误。
  • 如果 from 是符号链接,则
  • 如果 options 中存在 copy_options::skip_symlink,则不执行任何操作。
  • 否则,如果 to 不存在且 options 中存在 copy_options::copy_symlinks,则行为等价于 copy_symlink(from, to)
  • 否则,报告错误。
  • 否则,如果 from 是常规文件,则
  • 如果 options 中存在 copy_options::directories_only,则不执行任何操作。
  • 否则,如果 options 中存在 copy_options::create_symlinks,则创建指向 to 的符号链接。注意:除非 to 在当前目录中,否则 from 必须是绝对路径。
  • 否则,如果 options 中存在 copy_options::create_hard_links,则创建指向 to 的硬链接。
  • 否则,如果 to 是目录,则行为等价于 copy_file(from, to/from.filename(), options)(将 from 的副本作为文件创建在目录 to 中)。
  • 否则,行为等价于 copy_file(from, to, options)(拷贝文件)。
  • 否则,如果 from 是目录且 options 具有 copy_options::recursive 或为 copy_options::none
  • 如果 to 不存在,则首先执行 create_directory(to, from)(创建新目录并复制旧目录的属性)。
  • 然后,无论 to 是否已存在或刚刚创建,都通过 for (const directory_entry& x : directory_iterator(from)) 遍历 from 中包含的文件,对于每个目录条目,递归调用 copy(x.path(), to/x.path().filename(), options | unspecified),其中 unspecified 是一个特殊的位,当在 options 中设置时没有其他效果(设置此位的唯一目的是防止当 optionscopy_options::none 时递归拷贝子目录)。
  • 否则不执行任何操作。

目录

[编辑] 参数

from - 源文件、目录或符号链接的路径
to - 目标文件、目录或符号链接的路径
ec - 非抛出重载中用于错误报告的出参

[编辑] 返回值

(无)

[编辑] 异常

不带 error_code& 参数的重载在底层 OS API 错误时抛出 filesystem_error,构造时以 from 作为第一个参数,to 作为第二个参数,OS 错误码作为错误码参数。如果内存分配失败,可能会抛出 std::bad_alloc。带 error_code& 参数的重载在 OS API 调用失败时将其设置为 OS API 错误码,并且在没有错误发生时执行 ec.clear()。此重载具有
noexcept 规范:  
noexcept
  

[编辑] 注意

拷贝目录时的默认行为是非递归拷贝:文件被拷贝,但不包括子目录

// 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

而使用 copy_options::recursive 时,子目录及其内容也会被递归拷贝。

// ...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

[编辑] 示例

#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");
}

[编辑] 参阅

指定复制操作的语义
(枚举) [编辑]
复制一个符号链接
(函数) [编辑]
复制文件内容
(函数) [编辑]