Namespaces
Variants

std::experimental::filesystem:: canonical

From cppreference.net
ヘッダーで定義 <experimental/filesystem>
path canonical ( const path & p, const path & base = current_path ( ) ) ;
(1) (filesystem TS)
path canonical ( const path & p, error_code & ec ) ;
(2) (filesystem TS)
path canonical ( const path & p, const path & base, error_code & ec ) ;
(3) (filesystem 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"

関連項目

パスを表現する
(クラス)
絶対パスを構成する
パスを絶対パスに変換し、OS固有の動作を再現する
(関数)