名稱空間
變體
操作

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

來自 cppreference.com
 
 
 
定義於標頭檔案 <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)
標識檔案系統許可權
(列舉) [編輯]