[fs.class.directory.entry.general]

# 31 Input/output library [[input.output]](./#input.output)

## 31.12 File systems [[filesystems]](filesystems#fs.class.directory.entry.general)

### 31.12.10 Class directory_entry [[fs.class.directory.entry]](fs.class.directory.entry#general)

#### 31.12.10.1 General [fs.class.directory.entry.general]

[ð](#lib:directory_entry)

namespace std::filesystem {class directory_entry {public:// [[fs.dir.entry.cons]](fs.dir.entry.cons "31.12.10.2 Constructors"), constructors and destructor directory_entry() noexcept = default;
 directory_entry(const directory_entry&) = default;
 directory_entry(directory_entry&&) noexcept = default; explicit directory_entry(const filesystem::path& p);
 directory_entry(const filesystem::path& p, error_code& ec); ~directory_entry(); // assignments directory_entry& operator=(const directory_entry&) = default;
 directory_entry& operator=(directory_entry&&) noexcept = default; // [[fs.dir.entry.mods]](fs.dir.entry.mods "31.12.10.3 Modifiers"), modifiersvoid assign(const filesystem::path& p); void assign(const filesystem::path& p, error_code& ec); void replace_filename(const filesystem::path& p); void replace_filename(const filesystem::path& p, error_code& ec); void refresh(); void refresh(error_code& ec) noexcept; // [[fs.dir.entry.obs]](fs.dir.entry.obs "31.12.10.4 Observers"), observersconst filesystem::path& path() const noexcept; operator const filesystem::path&() const noexcept; bool exists() const; bool exists(error_code& ec) const noexcept; bool is_block_file() const; bool is_block_file(error_code& ec) const noexcept; bool is_character_file() const; bool is_character_file(error_code& ec) const noexcept; bool is_directory() const; bool is_directory(error_code& ec) const noexcept; bool is_fifo() const; bool is_fifo(error_code& ec) const noexcept; bool is_other() const; bool is_other(error_code& ec) const noexcept; bool is_regular_file() const; bool is_regular_file(error_code& ec) const noexcept; bool is_socket() const; bool is_socket(error_code& ec) const noexcept; bool is_symlink() const; bool is_symlink(error_code& ec) const noexcept;
 uintmax_t file_size() const;
 uintmax_t file_size(error_code& ec) const noexcept;
 uintmax_t hard_link_count() const;
 uintmax_t hard_link_count(error_code& ec) const noexcept;
 file_time_type last_write_time() const;
 file_time_type last_write_time(error_code& ec) const noexcept;
 file_status status() const;
 file_status status(error_code& ec) const noexcept;
 file_status symlink_status() const;
 file_status symlink_status(error_code& ec) const noexcept; bool operator==(const directory_entry& rhs) const noexcept;
 strong_ordering operator<=>(const directory_entry& rhs) const noexcept; // [[fs.dir.entry.io]](fs.dir.entry.io "31.12.10.5 Inserter"), insertertemplate<class charT, class traits>friend basic_ostream<charT, traits>&operator<<(basic_ostream<charT, traits>& os, const directory_entry& d); private: filesystem::path *path-object*; // *exposition only*};}

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16110)

A directory_entry object stores a path object
and may store additional objects for file attributes
such as hard link count, status, symlink status, file size, and last write time[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16116)

Implementations should store such additional file attributes
during directory iteration if their values are available
and storing the values would allow the implementation to eliminate file system accesses
by directory_entry observer functions ([[fs.op.funcs]](fs.op.funcs "31.12.13 Filesystem operation functions"))[.](#2.sentence-1)

Such stored file attribute values are said to be [*cached*](#def:file_attributes,cached "31.12.10.1 General [fs.class.directory.entry.general]")[.](#2.sentence-2)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16123)

[*Note [1](#note-1)*:

directory_iterator can cache
already available attribute values
directly into a directory_entry object
without the cost of a call to refresh()[.](#3.sentence-1)

â *end note*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L16131)

[*Example [1](#example-1)*: using namespace std::filesystem;

// use possibly cached last write time to minimize disk accessesfor (auto&& x : directory_iterator(".")){ std::cout << x.path() << " " << x.last_write_time() << std::endl;}// call refresh() to refresh a stale cachefor (auto&& x : directory_iterator(".")){ lengthy_function(x.path()); // cache becomes stale x.refresh();
 std::cout << x.path() << " " << x.last_write_time() << std::endl;}

On implementations that do not cache the last write time,
both loops will result in a potentially expensive call
to the std​::​filesystem​::​last_write_time function[.](#4.sentence-1)

On implementations that do cache the last write time,
the first loop will use the cached value and so
will not result in a potentially expensive call
to the std​::​filesystem​::​last_write_time function[.](#4.sentence-2)

The code is portable to any implementation,
regardless of whether or not it employs caching[.](#4.sentence-3)

â *end example*]
