Skip to content

Latest commit

 

History

History
99 lines (76 loc) · 2.51 KB

parent_path.md

File metadata and controls

99 lines (76 loc) · 2.51 KB

parent_path

  • filesystem[meta header]
  • std::filesystem[meta namespace]
  • path[meta class]
  • function[meta id-type]
  • cpp17[meta cpp]
path parent_path() const;

概要

親のパスを取得する。

パスにルートパスのみが含まれている場合は、ルートパスがそのまま返る。ルートパスの親は自身のパスであると見なされる。

戻り値

!has_relative_path()であれば*thisが返る。そうでなければ、汎用フォーマットのパスに対して、ディレクトリ区切りした要素のうち、末尾要素を除いたパスを返す。

POSIXベースシステムでの例

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
  fs::path ps[] = {
    "/foo/bar.txt", // ファイル名を含むパス
    "/foo/bar/",    // ディレクトリパス
    "/foo/bar",     // ディレクトリパス(末尾/なし)
    "/"             // ルートパスのみ (ルートパスの親はルートパスなのでそのまま返る)
  };

  for (const fs::path& p : ps) {
    std::cout << p << " : " << p.parent_path() << std::endl;
  }
}
  • parent_path()[color ff0000]

出力

"/foo/bar.txt" : "/foo"
"/foo/bar/" : "/foo/bar"
"/foo/bar" : "/foo"
"/" : "/"

Windowsでの例

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
  fs::path ps[] = {
    "C:/foo/bar.txt", // ファイル名を含むパス
    "C:/foo/bar/",    // ディレクトリパス
    "C:/"             // ルートパスのみ (ルートパスの親はルートパスなのでそのまま返る)
  };

  for (const fs::path& p : ps) {
    std::cout << p << " : " << p.parent_path() << std::endl;
  }
}
  • parent_path()[color ff0000]

出力

"C:/foo/bar.txt" : "C:/foo"
"C:/foo/bar/" : "C:/foo/bar"
"C:/" : "C:/"

バージョン

言語

  • C++17

処理系

参照