Home

dirent

Dirent, short for directory entry, is a portable API used by POSIX-compliant systems to read the contents of a directory. It is defined in the dirent.h header and centers on two main concepts: the DIR type, which represents an open directory stream, and the struct dirent, which represents a single directory entry. The d_name field stores the entry name, and many implementations also provide d_ino (inode number) and d_type (the entry’s file type, such as DT_DIR or DT_REG). The exact fields and their availability can vary between platforms; some fields may be absent or require additional checks.

The standard operations are opendir, readdir, and closedir. opendir opens a directory stream and returns a DIR*;

Portability and caveats: the dirent interface is defined by POSIX and is widely implemented on Linux, BSD,

Example usage pattern: open a directory with opendir, iterate with readdir to process each entry by name,

readdir
reads
successive
dirent
records
from
that
stream
until
NULL
is
returned,
indicating
end
of
entries
or
an
error.
closedir
closes
the
stream.
Additional
functions
include
rewinddir
(to
restart
reading
from
the
beginning)
and
seekdir/telldir
(to
reposition
within
the
stream).
Some
systems
offer
scandir,
a
higher-level
function
that
reads,
filters,
and
sorts
entries
into
an
array
of
dirent
pointers.
Readdir
may
return
entries
in
any
order,
and
entries
“.”
and
“..”
are
typically
included
unless
filtered
by
the
caller.
macOS,
and
other
UNIX-like
systems.
However,
the
availability
of
certain
fields
(notably
d_type)
and
the
exact
layout
of
struct
dirent
can
vary,
so
portable
code
often
checks
for
feature
support
and
may
fallback
to
stat
to
determine
entry
types.
and
finally
close
with
closedir.