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
処理系
- Clang:
- GCC: 8.1 ✅
- Visual C++: 2017 Update 7 ✅