命名空间
变体
操作

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()

[编辑] 注解

当递归复制目录时,属性保留重载 (3,4)copy() 隐式调用。它在 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++ 标准。

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

[编辑] 参见

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