最終更新日時(UTC):
が更新

履歴 編集

function
<filesystem>

std::filesystem::path::relative_path(C++17)

path relative_path() const;

概要

ルートパスからの相対パスを取得する。

戻り値

パスが空でない場合、ルートパスを除いたパスを返す。そうでなければ空のパスを返す。

POSIXベースシステムでの例

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
  fs::path p = "/usr/bin/clang";

  fs::path root_p = p.root_path();
  fs::path rel_p = p.relative_path();

  std::cout << root_p << std::endl;
  std::cout << rel_p << std::endl;
}

出力

"/"
"usr/bin/clang"

Windowsでの例

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
  fs::path p = "C:/Program Files/a.txt";

  fs::path root_p = p.root_path();
  fs::path rel_p = p.relative_path();

  std::cout << root_p << std::endl;
  std::cout << rel_p << std::endl;
}

出力

"C:/"
"Program Files/a.txt"

バージョン

言語

  • C++17

処理系