Spec-Zone.ru › C++

std::filesystem::directory_entry::path

const std::filesystem::path& path() const noexcept;
(since C++17)
operator const std::filesystem::path& () const noexcept;
(since C++17)

Возвращает полный путь, к которому относится запись каталога.

Параметры

(нет)

Возвращаемое значение

Полный путь, к которому относится запись каталога.

Пример

#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);
}

Возможный вывод:

two
two
one
one
three
three

См. также

path
(C++17)
представляет путь
(класс)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/filesystem/directory_entry/path

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API