Skip to content
Tutorials Center
My learning

Topics

C Reference ยท POSIX and Systems

C opendir(), readdir() and closedir() functions

Reference for opendir(), readdir() and closedir(): list the entries of a directory, skip . and .., sort the names, check types with stat, and handle errors.

opendir(), readdir() and closedir() from <dirent.h> read the contents of a directory. opendir returns a DIR * handle, each call to readdir returns a pointer to the next entry (a struct dirent with the entry name), and closedir releases the handle. When there are no more entries readdir returns NULL.

The order of entries is not defined: it depends on the file system and can change between runs. The listing also contains the special entries . (this directory) and .. (its parent). Programs that need stable output collect the names and sort them.

C
#include <dirent.h>

DIR *d = opendir(".");
struct dirent *e;
while ((e = readdir(d)) != NULL) { /* e->d_name */ }
closedir(d);

Try it

C Create files, list them in sorted order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Entry types, errors and a recursive count

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#include <dirent.h>

DIR *opendir(const char *path);
struct dirent *readdir(DIR *dir);
int closedir(DIR *dir);
void rewinddir(DIR *dir);
Name Description
path The directory to open.
dir The handle returned by opendir.

Returns: opendir returns a handle, or NULL on error (ENOENT, ENOTDIR, EACCES, EMFILE, ENFILE). readdir returns the next entry or NULL at the end of the directory; to tell an error from the end, set errno = 0 before the call and check it afterwards. closedir returns 0 or -1.

The dirent structure

POSIX guarantees only two members: d_name (the entry name, a null-terminated string) and d_ino (the inode number). Linux and BSD also provide d_type (DT_REG, DT_DIR, DT_LNK, DT_UNKNOWN), a free hint that saves a stat call. Some file systems return DT_UNKNOWN, so fall back to stat in that case.

Usage notes

  • Order is unspecified. Sort the names when you need repeatable output, as the first example does.
  • The pointer returned by readdir points into storage owned by the DIR; it is overwritten by the next call and freed by closedir. Copy the name (strdup) if you need it later.
  • readdir is not guaranteed to be thread-safe on the same DIR * stream. Use one stream per thread, or lock. The old readdir_r is deprecated.
  • d_name holds only the name; build the full path (dir + "/" + name) before calling stat or open.
  • Names can contain any byte except / and the null byte, including spaces, newlines and invalid UTF-8.
  • Changing the directory while reading it (creating or removing entries) may or may not show the changes.
  • Skip . and .. explicitly, or recursion will never end.
  • d_type does not follow symbolic links. A symbolic link to a directory is DT_LNK; use stat to follow it, and protect recursive walkers against cycles.
  • scandir reads and sorts in one call; nftw and fts walk whole trees.
  • Use fdopendir and openat to avoid races when a path may be replaced during traversal.