首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::create_directory

Defined in header <filesystem>

?

?

bool create_directory( const std::filesystem::path& p ); bool create_directory( const std::filesystem::path& p, std::error_code& ec );

(1)

(since C++17)

bool create_directory( const std::filesystem::path& p, const std::filesystem::path& existing_p ); bool create_directory( const std::filesystem::path& p, const std::filesystem::path& existing_p, std::error_code& ec );

(2)

(since C++17)

bool create_directories( const std::filesystem::path& p ); bool create_directories( const std::filesystem::path& p, std::error_code& ec );

(3)

(since C++17)

1%29创建目录p好像是由POSIXmkdir%28%29第二个论点是static_cast<int>(std::filesystem::perms::all)%28父目录必须已存在%29。如果p已经存在并且已经是一个目录,函数什么都不做。此条件不被视为错误%29。

2%29与%281%29相同,但新目录的属性是从existing_p%28必须是存在%29的目录。复制的属性与操作系统有关:在POSIX系统上,属性被复制为

二次

代码语言:javascript
复制
stat(existing_p.c_str(), &attributes_stat)
mkdir(p.c_str(), attributes_stat.st_mode)

二次

在Windows操作系统上,属性被复制,就好像

二次

代码语言:javascript
复制
CreateDirectoryExW(existing_p.c_str(), p.c_str(), 0)

二次

3%29执行人%281%29p这还不存在。

无抛过载返回false如果发生任何错误。

参数

p

-

the path to the new directory to create

existing_p

-

the path to a directory to copy the attributes from

ec

-

out-parameter for error reporting in the non-throwing overload

返回值

1,2%29true如果目录创建成功,false否则。

例外

1,3%29不需要std::error_code&参数抛文件系统[医]误差关于基础OS API错误,使用p作为第一个参数和操作系统错误代码作为错误代码参数。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载

noexcept规格:

noexcept

2%29不占用std::error_code&参数抛文件系统[医]误差关于基础OS API错误,使用p作为第一个论点,existing_p作为第二个参数,操作系统错误代码作为错误代码参数。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载

noexcept规格:

noexcept

注记

属性保持重载%282%29被隐式调用。copy()当递归复制目录时。它在助推中的作用是一样的。文件系统复制[医]目录%28与参数顺序颠倒%29。

二次

代码语言:javascript
复制
#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");
}

二次

可能的产出:

二次

代码语言:javascript
复制
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

二次

另见

create_symlinkcreate_directory_symlink (C++17)(C++17)

creates a symbolic link (function)

copy (C++17)

copies files or directories (function)

perms (C++17)

identifies file system permissions (enum)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com