Namespaces
Variants

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

From cppreference.net
定义于头文件 <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) 当底层操作系统 API 出错时抛出 std::filesystem::filesystem_error ,该异常以 p 作为第一个路径参数,以操作系统错误代码作为错误码参数构造。
2,4) 若操作系统 API 调用失败,将 std:: error_code & 参数设为操作系统 API 错误码;若未发生错误,则执行 ec. clear ( )

注释

函数 canonical() 的设计参照了 POSIX 标准中的 realpath 函数实现。

函数 weakly_canonical() 的引入是为了简化 relative() 的操作语义。

示例

#include <filesystem>
#include <iostream>
int main()
{
    /* 设置沙盒目录结构:
     a
     └── b
         ├── c1
         │   └── d <== 当前路径
         └── 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 << "当前路径为 "
              << std::filesystem::current_path() << '\n'
              << "路径 " << p1 << " 的规范路径为 "
              << std::filesystem::canonical(p1) << '\n'
              << "路径 " << p2 << " 的弱规范路径为 "
              << std::filesystem::weakly_canonical(p2) << '\n';
    try
    {
        [[maybe_unused]] auto x_x = std::filesystem::canonical(p2);
        // 不会执行到此处
    }
    catch (const std::exception& ex)
    {
        std::cout << "路径 " << p2 << " 的规范路径获取抛出异常:\n"
                  << ex.what() << '\n';
    }
    // 清理
    std::filesystem::current_path(old);
    const auto count = std::filesystem::remove_all(tmp / "a");
    std::cout << "已删除 " << count << " 个文件或目录。\n";
}

可能的输出:

当前路径为 "/tmp/a/b/c1/d"
路径 "../../c2/./e" 的规范路径为 "/tmp/a/b/c2/e"
路径 "../no-such-file" 的弱规范路径为 "/tmp/a/b/c1/no-such-file"
路径 "../no-such-file" 的规范路径获取抛出异常:
filesystem error: in canonical: No such file or directory [../no-such-file] [/tmp/a/b/c1/d]
已删除 6 个文件或目录。

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时的行为 正确行为
LWG 2956 C++17 canonical 函数存在冗余的 base 参数 已移除

参见

(C++17)
表示路径
(类)
(C++17)
组合绝对路径
(函数)
组合相对路径
(函数)