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.
#include <dirent.h> DIR *d = opendir("."); struct dirent *e; while ((e = readdir(d)) != NULL) { /* e->d_name */ } closedir(d);
Try it
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.
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
#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
readdirpoints into storage owned by theDIR; it is overwritten by the next call and freed byclosedir. Copy the name (strdup) if you need it later. readdiris not guaranteed to be thread-safe on the sameDIR *stream. Use one stream per thread, or lock. The oldreaddir_ris deprecated.d_nameholds only the name; build the full path (dir + "/" + name) before callingstatoropen.- 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_typedoes not follow symbolic links. A symbolic link to a directory isDT_LNK; usestatto follow it, and protect recursive walkers against cycles.scandirreads and sorts in one call;nftwandftswalk whole trees.- Use
fdopendirandopenatto avoid races when a path may be replaced during traversal.
Related
- stat, open, read, write, close, unistd.h
- qsort and errno.h
- Tutorial: System calls, File handling