定义于头文件
<filesystem>
|
||
bool create_directory( const std::filesystem::path& p );
bool create_directory( const std::filesystem::path& p, std::error_code& ec ); |
(1) | (C++17 起) |
bool create_directory( const std::filesystem::path& p,
const std::filesystem::path& existing_p ); |
(2) | (C++17 起) |
bool create_directories( const std::filesystem::path& p );
bool create_directories( const std::filesystem::path& p, |
(3) | (C++17 起) |
p
(亲目录必须已经存在)。若 p
已经存在且已是目录,则函数无操作(此条件不视为错误)。existing_p
(必须是已存在的目录)。复制的属性是依赖操作系统的:在 POSIX 系统上,如同按照下方复制属性
stat(existing_p.c_str(), &attributes_stat) mkdir(p.c_str(), attributes_stat.st_mode)
CreateDirectoryExW(existing_p.c_str(), p.c_str(), 0)
p
的元素执行 (1) 。若发生任何错误,则不抛出重载返回 false 。
目录 |
p | - | 要创建的新目录的路径 |
existing_p | - | 要自之复制属性的目录的路径 |
ec | - | 不抛出重载中报告错误的输出参数 |
p
和作为错误码参数的 OS 错误码构造。若内存分配失败则可能抛出 std::bad_alloc 。若 OS API 调用失败,则接受 std::error_code& 参数的重载设置该参数为 OS API 错误码,而若不出现错误则执行 ec.clear() 。p
,第二参数 existing_p
和作为错误码参数的 OS 错误码构造。若内存分配失败则可能抛出 std::bad_alloc 。若 OS API 调用失败,则接受 std::error_code& 参数的重载设置该参数为 OS API 错误码,而若不出现错误则执行 ec.clear() 。保持属性的重载 (2) 被 copy() 在递归地复制目录时隐式调用。其在 boost.filesystem 的等价物是 copy_directory (并保持参数顺序)
#include <iostream> #include <fstream> #include <cstdlib> #include <filesystem> namespace fs = std::filesystem; int main() { fs::create_directories("sandbox/1/2/a"); fs::create_directory("sandbox/1/2/b"); fs::permissions("sandbox/1/2/b", fs::perms::remove_perms | fs::perms::others_all); fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b"); std::system("ls -l sandbox/1/2"); fs::remove_all("sandbox"); }
可能的输出:
drwxr-xr-x 2 user group 4096 Apr 15 09:33 a drwxr-x--- 2 user group 4096 Apr 15 09:33 b drwxr-x--- 2 user group 4096 Apr 15 09:33 c
(C++17)
(C++17) |
创建一个符号链接 (函数) |
(C++17)
|
复制文件或目录 (函数) |
(C++17)
|
标识文件系统权限 (枚举) |