名稱空間
變體
操作

std::filesystem::canonical, std::filesystem::weakly_canonical

來自 cppreference.com
 
 
 
定義於標頭檔案 <filesystem>
path canonical( const std::filesystem::path& p );
(1) (C++17 起)
path canonical( const std::filesystem::path& p,
                std::error_code& ec );
(2) (C++17 起)
path weakly_canonical( const std::filesystem::path& p );
(3) (C++17 起)
path weakly_canonical( const std::filesystem::path& p,
                       std::error_code& ec );
(4) (C++17 起)
1,2) 將路徑 p 轉換為規範的絕對路徑,即在其通用格式表示中沒有點、點點元素或符號連結的絕對路徑。如果 p 不是絕對路徑,則該函式的操作就如同它首先透過 std::filesystem::absolute(p) 變為絕對路徑一樣。路徑 p 必須存在。
3,4) 返回一個由 operator/= 構成的路徑,其結果來自對 p 中存在的(由 status(p)status(p, ec) 確定)前導元素組成的路徑呼叫 canonical(),如果存在的話,後面跟著 p 中不存在的元素。生成的路徑為標準形式

目錄

[編輯] 引數

p - 一個可以是絕對路徑或相對路徑的路徑;對於 canonical,它必須是現有路徑
ec - 用於儲存錯誤狀態的錯誤程式碼

[編輯] 返回值

1,2) 解析為與 std::filesystem::absolute(p) 相同檔案的絕對路徑。
3,4) 形式為 canonical(x)/y 的標準路徑,其中 x 是由 p 中最長的現有元素序列組成的路徑,而 y 是由 p 中剩餘的非現有尾部元素組成的路徑。

[編輯] 異常

任何未標記為 noexcept 的過載都可能在記憶體分配失敗時丟擲 std::bad_alloc

1,3) 在底層 OS API 錯誤時丟擲 std::filesystem::filesystem_error,使用 p 作為第一個路徑引數,OS 錯誤碼作為錯誤碼引數來構造。
2,4) 如果 OS API 呼叫失敗,將 std::error_code& 引數設定為 OS API 錯誤碼;如果沒有發生錯誤,則執行 ec.clear()

[編輯] 注意

函式 canonical() 仿照 POSIX realpath

函式 weakly_canonical() 的引入是為了簡化 relative() 的操作語義。

[編輯] 示例

#include <filesystem>
#include <iostream>
 
int main()
{
    /* set up sandbox directories:
     a
     └── b
         ├── c1
         │   └── d <== current path
         └── c2
             └── e
    */
    auto old = std::filesystem::current_path();
    auto tmp = std::filesystem::temp_directory_path();
    std::filesystem::current_path(tmp);
    auto d1 = tmp / "a/b/c1/d";
    auto d2 = tmp / "a/b/c2/e";
    std::filesystem::create_directories(d1);
    std::filesystem::create_directories(d2);
    std::filesystem::current_path(d1);
 
    auto p1 = std::filesystem::path("../../c2/./e");
    auto p2 = std::filesystem::path("../no-such-file");
    std::cout << "Current path is "
              << std::filesystem::current_path() << '\n'
              << "Canonical path for " << p1 << " is "
              << std::filesystem::canonical(p1) << '\n'
              << "Weakly canonical path for " << p2 << " is "
              << std::filesystem::weakly_canonical(p2) << '\n';
    try
    {
        [[maybe_unused]] auto x_x = std::filesystem::canonical(p2);
        // NOT REACHED
    }
    catch (const std::exception& ex)
    {
        std::cout << "Canonical path for " << p2 << " threw exception:\n"
                  << ex.what() << '\n';
    }
 
    // cleanup
    std::filesystem::current_path(old);
    const auto count = std::filesystem::remove_all(tmp / "a");
    std::cout << "Deleted " << count << " files or directories.\n";
}

可能的輸出

Current path is "/tmp/a/b/c1/d"
Canonical path for "../../c2/./e" is "/tmp/a/b/c2/e"
Weakly canonical path for "../no-such-file" is "/tmp/a/b/c1/no-such-file"
Canonical path for "../no-such-file" threw exception:
filesystem error: in canonical: No such file or directory [../no-such-file] [/tmp/a/b/c1/d]
Deleted 6 files or directories.

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2956 C++17 canonical 有一個多餘的 base 引數 已移除

[編輯] 參見

(C++17)
表示一個路徑
(類) [編輯]
(C++17)
組成一個絕對路徑
(函式) [編輯]
組成一個相對路徑
(函式) [編輯]