The GNU C Library - Accessing Directories

Node: Accessing Directories Next: Hard Links Prev: Working Directory Up: File System Interface

Accessing Directories

The facilities described in this section let you read the contents of a directory file. This is useful if you want your program to list all the files in a directory, perhaps as part of a menu.

The opendir function opens a directory stream whose elements are directory entries. You use the readdir function on the directory stream to retrieve these entries, represented as struct dirent objects. The name of the file for each entry is stored in the d_name member of this structure. There are obvious parallels here to the stream facilities for ordinary files, described in I/O on Streams.

Directory Entries Format of one directory entry.
Opening a Directory How to open a directory stream.
Reading/Closing Directory How to read directory entries from the stream.
Simple Directory Lister A very simple directory listing program.
Random Access Directory Rereading part of the directory
already read with the same stream.


Next: Hard Links Up: File System Interface