std::experimental::filesystem:: create_directory, std::experimental::filesystem:: create_directories
From cppreference.net
<
cpp
|
experimental
|
fs
|
定义于头文件
<experimental/filesystem>
|
||
|
bool
create_directory
(
const
path
&
p
)
;
bool create_directory ( const path & p, error_code & ec ) ; |
(1) | (文件系统 TS) |
|
bool
create_directory
(
const
path
&
p,
const
path
&
existing_p
)
;
bool create_directory ( const path & p, const path & existing_p, error_code & ec ) ; |
(2) | (文件系统 TS) |
|
bool
create_directories
(
const
path
&
p
)
;
bool create_directories ( const path & p, error_code & ec ) ; |
(3) | (文件系统 TS) |
1)
创建目录
p
,其行为类似于 POSIX
mkdir()
函数的第二个参数为
static_cast
<
int
>
(
fs
::
perms
::
all
)
(父目录必须已存在)。如果
p
已存在且已经是目录,则该函数不执行任何操作(此情况不被视为错误)。
2)
与
(1)
相同,区别在于新目录的属性从
existing_p
(必须是已存在的目录)复制。具体复制哪些属性取决于操作系统:在POSIX系统上,属性复制方式相当于
在Windows操作系统上,属性复制方式相当于
stat(existing_p.c_str(), &attributes_stat) mkdir(p.c_str(), attributes_stat.st_mode)
CreateDirectoryExW(existing_p.c_str(), p.c_str(), 0)
3)
对
p
中尚不存在的每个元素执行
(1)
。
不抛出异常的重载在出现任何错误时返回 false 。
目录 |
参数
| p | - | 要创建的新目录路径 |
| existing_p | - | 用于复制属性的源目录路径 |
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
1,2)
true
若目录创建成功,
false
否则。
异常
1,3)
不接受
error_code
&
参数的重载在底层操作系统 API 出错时会抛出
filesystem_error
,该异常以
p
作为第一参数,操作系统错误代码作为错误码参数构造。若内存分配失败则可能抛出
std::
bad_alloc
。接受
error_code
&
参数的重载在操作系统 API 调用失败时将其设置为对应的错误码,若无错误发生则执行
ec.
clear
(
)
。此重载具有
noexcept
规范:
noexcept
2)
不接受
error_code
&
参数的重载版本在底层操作系统 API 出错时会抛出
filesystem_error
异常,该异常以
p
作为第一参数、
existing_p
作为第二参数,并将操作系统错误代码作为错误码参数进行构造。若内存分配失败则可能抛出
std::
bad_alloc
。接受
error_code
&
参数的重载版本在操作系统 API 调用失败时将其设置为对应的错误码,若无错误发生则执行
ec.
clear
(
)
。此重载版本具有
noexcept
规范:
noexcept
注释
属性保留重载 (2) 在递归复制目录时会被 copy() 隐式调用。其在boost.filesystem中的等效函数是 copy_directory (参数顺序相反)。
示例
运行此代码
#include <cstdlib> #include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::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
参见
|
创建符号链接
(函数) |
|
|
复制文件或目录
(函数) |
|
|
标识文件系统权限
(枚举) |