命名空间
变体
操作

std::experimental::filesystem::copy_file

来自 cppreference.cn
< cpp‎ | experimental‎ | fs
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行性扩展 (parallelism TS)
并行性扩展 2 (parallelism TS v2)
并发性扩展 (concurrency TS)
并发性扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非TS
模式匹配
线性代数
std::execution
契约
2D图形
 
 
定义于头文件 <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) 默认情况,等同于 (2),使用 copy_options::none 作为 options
2) 将单个文件从 from 复制到 to,使用 options 指示的复制选项。 如果在 copy_options 选项组中的任何一个选项组中存在多个选项(即使在与 copy_file 无关的组中),则行为未定义。
  • 如果目标文件不存在,
  • 复制 from 解析到的文件的内容和属性到 to 解析到的文件(符号链接会被跟随)。
  • 否则,如果目标文件已存在
  • 如果 tofromequivalent(from, to) 确定为相同,则报告错误。
  • 否则,如果在 options 中未设置任何 copy_file 控制选项,则报告错误。
  • 否则,如果在 options 中设置了 copy_options::skip_existing,则不执行任何操作。
  • 否则,如果在 options 中设置了 copy_options::overwrite_existing,则将 from 解析到的文件的内容和属性复制到 to 解析到的文件。
  • 否则,如果在 options 中设置了 copy_options::update_existing,则仅当 fromto 新时才复制文件,由 last_write_time() 定义。

无异常抛出的重载版本在发生错误时返回 false

目录

[编辑] 参数

from - 源文件的路径
to - 目标文件的路径
ec - 非抛出重载中的错误报告输出参数

[编辑] 返回值

true 如果文件被复制,否则为 false

[编辑] 异常

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

[编辑] 注解

这些函数最多涉及一次直接或间接调用 status(to)(用于确定文件是否存在,以及对于 copy_options::update_existing 选项,其最后写入时间)。

copy_file 用于复制目录时会报告错误:请使用 copy

copy_file 会跟随符号链接:对于符号链接的复制,请使用 copy_symlink 或带有 copy_options::copy_symlinkscopy

[编辑] 示例

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

可能的输出

file1.txt holds : a
file2.txt holds : a
Could not copy sandbox/abc: copy_file: Is a directory: "sandbox/abc", "sandbox/def"

[编辑] 参见

指定复制操作的语义
(枚举) [编辑]
复制符号链接
(函数) [编辑]
复制文件或目录
(函数) [编辑]