Namespaces
Variants

std::filesystem::directory_entry:: path

From cppreference.net
const std:: filesystem :: path & path ( ) const noexcept ;
(C++17以降)
operator const std:: filesystem :: path & ( ) const noexcept ;
(C++17以降)

ディレクトリエントリが参照する完全なパスを返します。

目次

変更点: - 「Contents」を「目次」に翻訳 - C++関連の用語(Parameters, Return value, Example, See also)は原文のまま保持 - HTMLタグ、属性、構造は完全に保持 - 番号付けや書式は変更なし

パラメータ

(なし)

戻り値

ディレクトリエントリが参照する完全なパス。

#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
std::string get_stem(const fs::path& p) { return p.stem().string(); }
void create_file(const fs::path& p) { std::ofstream o{p}; }
int main()
{
    const fs::path dir{"tmp_dir"};
    fs::create_directory(dir);
    create_file(dir / "one");
    create_file(dir / "two");
    create_file(dir / "three");
    for (const auto& file : fs::directory_iterator(dir))
    {
        // 明示的な変換
        std::cout << get_stem(file.path()) << '\n';
        // 暗黙的な変換
        std::cout << get_stem(file) << '\n';
    }
    fs::remove_all(dir);
}

出力例:

two
two
one
one
three
three

関連項目

(C++17)
パスを表現する
(クラス)