Reading a directory consists of the following steps (see the man pages for further details):
DIR *and which will subsequently be used by other code to read and process the contents of the directory. Make sure you check the return value of opendir to determine whether the directory was opened successfully.
struct dirent *(this is discussed below in more detail). readdir does its own bookkeeping internally and keeps track of how much of the directory it has read (and, therefore, what the next entry is), so you don't have to update the directory stream yourself in any way.
Your code might therefore look something like this:
DIR *dir_ptr;}
struct dirent *dirent_ptr;
...
dir_ptr = opendir(filename);
...
for ( ... ) {
dirent_ptr = readdir(dir_ptr);... process dirent_ptr ...
This is a variable length structure (because the length of the filename is not fixed), which is not easy to define in C. This is handled by using the field d_name to specify the address of the file name, and d_reclen to specify the length of the file name. (The code that allocates these structures makes sure that enough memory is allocated at d_name to hold the file name string, not just a single byte as the declaration seems to suggest.) The name of the file corresponding to the directory entry can therefore be obtained using the field d_name.struct dirent { ino_t d_ino; off_t d_off; unsigned short d_reclen; char d_name[1]; };