Namespaces
Variants
Actions

std::filesystem::directory_entry::path

From cppreference.com
 
C++
 
Filesystem library
Classes
Functions
File types
 
std::filesystem::directory_entry
const std::filesystem::path& path() const noexcept;
(since C++17)
operator const std::filesystem::path& () const noexcept;
(since C++17)

Returns the full path the directory entry refers to.

Contents

[edit] Parameters

(none)

[edit] Return value

The full path the directory entry refers to.

[edit] Example

#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))
    {
        / Explicit conversion
        std::cout << get_stem(file.path() << '\n';
 
        / Implicit conversion
        std::cout << get_stem(file) << '\n';
    }
 
    fs::remove_all(dir);
}

Possible output:

two
two
one
one
three
three

[edit] See also

(C++17)
represents a path
(class) [edit]

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant