Namespaces
Variants

std::experimental::filesystem:: absolute, std::experimental::filesystem:: system_complete

From cppreference.net
ヘッダーで定義 <experimental/filesystem>
path absolute ( const path & p, const path & base = current_path ( ) ) ;
(1) (filesystem TS)
path system_complete ( const path & p ) ;
path system_complete ( const path & p, error_code & ec ) ;
(2) (filesystem TS)
1) p の絶対パスを base からの相対パスとして返します。以下の規則に従います:
  • p がルート名とルートディレクトリの両方を持つ場合(例: "C:\users" )、パスは変更されずに返されます。
  • p がルート名を持ち、ルートディレクトリが続かない場合(例: "C:text.txt" )、 base p のルート名と残りの部分の間に挿入されます。形式的には、 p. root_name ( ) / fs :: absolute ( base ) . root_directory ( ) / fs :: absolute ( base ) . relative_path ( ) / p. relative_path ( ) が返されます。
  • p がルート名を持たず、ルートディレクトリを持つ場合(例:POSIXシステムでの "/var/tmp/file.txt" またはWindowsでの "\users \A BC\Document.doc" )、 base のルート名(存在する場合)が p の前に追加されます(POSIXシステムでは p は変更されません。Windowsシステムでは "\users \A BC\Document.doc" "C:\users \A BC\Document.doc" になります)。形式的には、 fs :: absolute ( base ) . root_name ( ) / p が返されます。
  • p がルート名もルートディレクトリも持たない場合(例: "../file.txt" )、 base 全体が p の前に追加されます。形式的には、 absolute ( base ) / p が返されます。
2) 指定されたパス名 p に対してOSのファイルオープンAPIがアクセスするファイルを特定する絶対パスを取得します。POSIXシステムでは、これはデフォルトの base ( fs::current_path() )を使用した (1) と同等です。Windowsシステムでは、各論理ドライブが独自のカレントワーキングディレクトリを持つため、 p がまだ絶対パスでなく、ルート名コンポーネントを持つ場合(例: "E:filename.txt" )、そのドライブのカレントワーキングディレクトリが使用されます。これは以前に実行されたプログラムによって設定されている可能性があります。

目次

パラメータ

p - 絶対形式に変換するパス
base - 開始位置として使用するパス(必ずしも絶対パスである必要はありません)
ec - 例外を送出しないオーバーロードでのエラー報告用出力パラメータ

戻り値

上記のように p base を組み合わせて形成された絶対パス(必ずしも正規化されているとは限らない)を返します。

例外

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

注記

ルート名をサポートするシステム(例:Windows)では、相対パスに対して absolute を呼び出した結果、そのパスにルート名が含まれている場合(例: "D:file.txt" )、 base のルート名が異なる場合、通常は存在しないパスが生成されます。

#include <filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::path p = "C:cl.exe";
    std::cout << "Current path is " << fs::current_path() << '\n'
              << "Absolute path for " << p << " is " << fs::absolute(p) << '\n'
	      << "System complete path for " << p << " is "
              << fs::system_complete(p) << '\n';
}

出力例:

Current path is "D:/local/ConsoleApplication1"
Absolute path for "C:cl.exe" is "C:/local/ConsoleApplication1/cl.exe"
System complete path for "C:cl.exe" is "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe"

関連項目

正規パスを構成する
(関数)