std::experimental::filesystem:: copy
From cppreference.net
<
cpp
|
experimental
|
fs
|
定义于头文件
<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_symlinks或copy_options::create_symlinks,则通过调用symlink_status)获取 from 的类型和权限。 - 如有必要,以相同方式通过不超过一次status或symlink_status调用来获取 to 的状态。
- 如果 from 不存在,则报告错误。
- 如果通过 equivalent() 确定 from 和 to 是同一文件,则报告错误。
- 如果通过 is_other 确定 from 或 to 不是常规文件、目录或符号链接,则报告错误。
- 如果 from 是目录,但 to 是常规文件,则报告错误。
- 如果 from 是符号链接,则
-
-
若
copy_options::skip_symlink存在于 options 中,则不执行任何操作。 -
否则,若
to
不存在且
copy_options::copy_symlinks存在于 options 中,则行为如同执行 copy_symlink ( from, to ) 。 - 否则,报告错误。
-
若
- 否则,如果 from 是常规文件,则
-
-
若
copy_options::directories_only存在于 options 中,则不执行任何操作。 -
否则,若
copy_options::create_symlinks存在于 options 中,则创建指向 to 的符号链接。注意:除非 to 位于当前目录下,否则 from 必须是绝对路径。 -
否则,若
copy_options::create_hard_links存在于 options 中,则创建指向 to 的硬链接。 - 否则,若 to 是目录,则行为等同于 copy_file ( from, to / from. filename ( ) , options ) (在目录 to 中创建 from 的副本文件)。
- 否则,行为等同于 copy_file ( from, to, options ) (复制文件)。
-
若
-
否则,如果
from
是一个目录,并且
options
包含
copy_options::recursive或者是copy_options::none。
-
- 如果 to 不存在,首先执行 create_directory ( to, from ) (创建新目录并复制旧目录的属性)。
-
然后,无论
to
是已存在还是刚被创建,都会遍历
from
中包含的文件,如同通过
for
(
const
directory_entry
&
x
:
directory_iterator
(
from
)
)
进行遍历,并对每个目录条目递归调用
copy
(
x.
path
(
)
, to
/
x.
path
(
)
.
filename
(
)
, options
|
unspecified
)
,其中
unspecified
是一个特殊位,当在
options
中设置时没有其他效果(设置此位的唯一目的是防止在
options
为
copy_options::none时递归复制子目录)。
- 否则不执行任何操作。
目录 |
参数
| from | - | 源文件、目录或符号链接的路径 |
| to | - | 目标文件、目录或符号链接的路径 |
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
(无)
异常
The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with from 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 an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept
规范:
noexcept
注释
复制目录时的默认行为是非递归复制:文件会被复制,但子目录不会被复制:
// 给定条件 // /dir1 包含 /dir1/file1, /dir1/file2, /dir1/dir2 // 且 /dir1/dir2 包含 /dir1/dir2/file3 // 执行后 std::experimental::filesystem::copy("/dir1", "/dir3"); // /dir3 被创建(继承 /dir1 的属性) // /dir1/file1 被复制到 /dir3/file1 // /dir1/file2 被复制到 /dir3/file2
当使用
copy_options::recursive
时,子目录也会被递归复制,包括其内容。
// ...但在执行后 std::experimental::filesystem::copy("/dir1", "/dir3", copy_options::recursive); // /dir3 被创建(具有 /dir1 的属性) // /dir1/file1 被复制到 /dir3/file1 // /dir1/file2 被复制到 /dir3/file2 // /dir3/dir2 被创建(具有 /dir1/dir2 的属性) // /dir1/dir2/file3 被复制到 /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"); // 复制文件 fs::copy("sandbox/dir", "sandbox/dir2"); // 复制目录(非递归) // sandbox 包含 2 个文件和 2 个目录,其中一个目录包含子目录 // sandbox/file1.txt // sandbox/file2.txt // sandbox/dir2 // sandbox/dir // sandbox/dir/subdir fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive); // sandbox/copy 包含上述文件和子目录的副本 fs::remove_all("sandbox"); }
参见
|
指定复制操作的语义
(枚举) |
|
|
复制符号链接
(函数) |
|
|
复制文件内容
(函数) |