Namespaces
Variants

std::experimental::filesystem:: canonical

From cppreference.net
定义于头文件 <experimental/filesystem>
path canonical ( const path & p, const path & base = current_path ( ) ) ;
(1) (文件系统 TS)
path canonical ( const path & p, error_code & ec ) ;
(2) (文件系统 TS)
path canonical ( const path & p, const path & base, error_code & ec ) ;
(3) (文件系统 TS)

将路径 p 转换为规范绝对路径,即不包含点号、双点号元素或符号链接的绝对路径。

如果 p 不是绝对路径,该函数的行为会先通过 absolute ( p, base ) absolute ( p ) 将其转换为绝对路径,对应 (2) 的情况。

路径 p 必须存在。

目录

参数

p - 一个可以是绝对路径或相对于 base 的路径,且必须是已存在的路径
base - p 为相对路径时使用的基准路径
ec - 用于存储错误状态的状态码

返回值

一个绝对路径,其解析结果与 absolute ( p, base ) (对于 (2) 版本则是 absolute ( p ) )所解析的文件相同。

异常

The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument, base as the second argument, and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept 规范:
noexcept

此函数基于POSIX realpath 规范实现。

示例

#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::path p = fs::path("..") / ".." / "AppData";
    std::cout << "Current path is " << fs::current_path() << '\n'
              << "Canonical path for " << p << " is " << fs::canonical(p) << '\n';
}

可能的输出:

Current path is "C:\Users\abcdef\AppData\Local\Temp"
Canonical path for "..\..\AppData" is "C:\Users\abcdef\AppData"

参见

表示路径
(类)
组合绝对路径
将路径转换为绝对路径并复制操作系统特定行为
(函数)