std::filesystem::copy_options
来自 cppreference.cn
< cpp | filesystem
定义于头文件 <filesystem> |
||
enum class copy_options { none = /* unspecified */, |
(自 C++17 起) | |
此类型表示可用的选项,这些选项控制 copy() 和 copy_file() 函数的行为。
copy_options
满足 BitmaskType 的要求(这意味着为该类型定义了按位运算符 operator&、 operator|、 operator^、 operator~、 operator&=、 operator|= 和 operator^=)。 none
表示空位掩码;每个其他枚举器表示不同的位掩码元素。
[编辑] 成员常量
在以下每个选项组中,最多可以存在一个复制选项,否则复制函数的行为是未定义的。
成员常量 | 含义 |
---|---|
控制 copy_file() 在文件已存在时的选项 | |
none
|
报告错误(默认行为)。 |
skip_existing
|
保留现有文件,不报告错误。 |
overwrite_existing
|
替换现有文件。 |
update_existing
|
仅当现有文件比正在复制的文件旧时才替换现有文件。 |
控制 copy() 对子目录效果的选项 | |
none
|
跳过子目录(默认行为)。 |
recursive
|
递归复制子目录及其内容。 |
控制 copy() 对符号链接效果的选项 | |
none
|
跟随符号链接(默认行为)。 |
copy_symlinks
|
将符号链接复制为符号链接,而不是它们指向的文件。 |
skip_symlinks
|
忽略符号链接。 |
控制 copy() 执行的复制类型的选项 | |
none
|
复制文件内容(默认行为)。 |
directories_only
|
复制目录结构,但不复制任何非目录文件。 |
create_symlinks
|
不创建文件副本,而是创建指向原始文件的符号链接。注意:源路径必须是绝对路径,除非目标路径位于当前目录中。 |
create_hard_links
|
不创建文件副本,而是创建解析为与原始文件相同文件的硬链接。 |
[编辑] 示例
运行此代码
#include <cstdlib> #include <filesystem> #include <fstream> #include <iostream> namespace fs = std::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) const auto copyOptions = fs::copy_options::update_existing | fs::copy_options::recursive | fs::copy_options::directories_only ; fs::copy("sandbox", "sandbox_copy", copyOptions); static_cast<void>(std::system("tree")); fs::remove_all("sandbox"); fs::remove_all("sandbox_copy"); }
可能的输出
. ├── sandbox │ ├── dir │ │ └── subdir │ ├── dir2 │ ├── file1.txt │ └── file2.txt └── sandbox_copy ├── dir │ └── subdir └── dir2 8 directories, 2 files
[编辑] 参见
(C++17) |
复制文件或目录 (函数) |
(C++17) |
复制文件内容 (函数) |