在线文档教程
C++
文件系统 | Filesystem

std::filesystem::canonical

文件系统::规范化的,std::文件系统::弱[医]典范

Defined in header
path canonical( const std::filesystem::path& p, const std::filesystem::path& base = std::filesystem::current_path() (1)(since C++17)
path canonical( const std::filesystem::path& p, std::error_code& ec (2)(since C++17)
path canonical( const std::filesystem::path& p, const std::filesystem::path& base, std::error_code& ec (3)(since C++17)
path weakly_canonical(const std::filesystem::path& p(4)(since C++17)
path weakly_canonical(const std::filesystem::path& p, std::error_code& ec(5)(since C++17)

1-3%29转换路径p对于典型的绝对路径,即没有点、点元素或符号链接的绝对路径。如果p不是绝对路径,函数的行为就好像它首先是由absolute(p, base)absolute(p)%282%29。小径p一定存在。

4-5%29返回由operator/=从打电话的结果canonical()没有基参数,并且使用由主元素组成的路径参数。p所确定的存在%28 As的status(p)status(p, ec)%29,如果有的话,后面跟着p范式...

参数

p-a path which may be absolute or relative to base, and which must be an existing path
base-base path to be used in case p is relative
ec-error code to store error status to

返回值

absolute(p, base)%28或absolute(p)%282%29%29。

4-5%29形式的正常路径canonical(x)/y

例外

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

noexcept规格:

noexcept

注记

功能canonical()真实路径...

功能weakly_canonical()的操作语义。relative()...

二次

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

二次

可能的产出:

二次

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

二次

另见

path (C++17)represents a path (class)
absolutesystem_complete (C++17)(C++17)composes an absolute pathconverts a path to an absolute path replicating OS-specific behavior (function)
relativeproximate (C++17)composes a relative path (function)

© cppreference.com

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

http://en.cppreference.com/w/cpp/filesystem/canonical