命名空间
变体
操作

std::filesystem::create_directory, std::filesystem::create_directories

来自 cppreference.cn
 
 
 
定义于头文件 <filesystem>
bool create_directory( const std::filesystem::path& p );
(1) (C++17 起)
bool create_directory( const std::filesystem::path& p, std::error_code& ec ) noexcept;
(2) (C++17 起)
bool create_directory( const std::filesystem::path& p,
                       const std::filesystem::path& existing_p );
(3) (C++17 起)
bool create_directory( const std::filesystem::path& p,

                       const std::filesystem::path& existing_p,

                       std::error_code& ec ) noexcept;
(4) (C++17 起)
bool create_directories( const std::filesystem::path& p );
(5) (C++17 起)
bool create_directories( const std::filesystem::path& p, std::error_code& ec );
(6) (C++17 起)
1,2) 创建目录 p,如同通过 POSIX mkdir() 并以 static_cast<int>(std::filesystem::perms::all) 为第二个参数(父目录必须已存在)。如果函数因 p 解析为现有目录而失败,则不报告错误。否则在失败时报告错误。
3,4)(1,2) 相同,但新目录的属性从 existing_p(必须是已存在的目录)复制。复制哪些属性取决于操作系统:在 POSIX 系统上,属性的复制方式如同
stat(existing_p.c_str(), &attributes_stat)
mkdir(p.c_str(), attributes_stat.st_mode)
在 Windows 操作系统上,不复制 existing_p 的任何属性。
5,6) 对于 p 的每个不存在的元素执行 (1,2)。如果 p 已存在,函数不做任何事情(此条件不被视为错误)。

目录

[编辑] 参数

p - 要创建的新目录的路径
existing_p - 从中复制属性的目录路径
ec - 非抛出重载中用于错误报告的出参

[编辑] 返回值

如果为目录 p 解析到的目标新创建了目录,则为 true,否则为 false

[编辑] 异常

任何未标记为 noexcept 的重载都可能在内存分配失败时抛出 std::bad_alloc

1,5) 在底层操作系统 API 错误时抛出 std::filesystem::filesystem_error,使用 p 作为第一个路径参数,操作系统错误码作为错误码参数构造。
2,6) 如果操作系统 API 调用失败,将 std::error_code& 参数设置为操作系统 API 错误码;如果未发生错误,则执行 ec.clear()
3) 在底层操作系统 API 错误时抛出 std::filesystem::filesystem_error,使用 p 作为第一个路径参数,existing_p 作为第二个路径参数,操作系统错误码作为错误码参数构造。
4) 如果操作系统 API 调用失败,将 std::error_code& 参数设置为操作系统 API 错误码;如果未发生错误,则执行 ec.clear()

[编辑] 注意

当递归复制目录时,copy() 会隐式调用保留属性的重载 (3,4)。它在 boost.filesystem 中的等价物是 copy_directory(参数顺序颠倒)。

[编辑] 示例

#include <cassert>
#include <cstdlib>
#include <filesystem>
 
int main()
{
    std::filesystem::current_path(std::filesystem::temp_directory_path());
 
    // Basic usage
    std::filesystem::create_directories("sandbox/1/2/a");
    std::filesystem::create_directory("sandbox/1/2/b");
 
    // Directory already exists (false returned, no error)
    assert(!std::filesystem::create_directory("sandbox/1/2/b"));
 
    // Permissions copying usage
    std::filesystem::permissions(
        "sandbox/1/2/b",
        std::filesystem::perms::others_all,
        std::filesystem::perm_options::remove
    );
    std::filesystem::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
 
    std::system("ls -l sandbox/1/2");
    std::system("tree sandbox");
    std::filesystem::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
sandbox
└── 1
    └── 2
        ├── a
        ├── b
        └── c

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 2935 C++17 如果目标已存在但不是目录,则出错 不作为错误
LWG 3014 C++17 create_directorieserror_code 重载标记为 noexcept 但可能分配内存 noexcept 已移除
P1164R1 C++17 由现有非目录文件导致的创建失败不作为错误 已作为错误

[编辑] 参阅

创建符号链接
(函数) [编辑]
(C++17)
复制文件或目录
(函数) [编辑]
(C++17)
标识文件系统权限
(枚举) [编辑]