File System

File System readdir

readdir

// INCLUDE
#include <dirent.h>

// PROTOTYPE
struct dirent* readdir(DIR* dirp) 

Reads the next entry from a directory. Used to find the names of all of the files and directories within a directory. See also readdir_r.

  • dirp: The DIR* returned by opendir.

Returns a pointer to a struct dirent containing information about the next file in the directory. Returns NULL when the end of the directory is reached. Returns NULL and sets errno if an error occurs.

Not all fields of the struct dirent are filled in. You should only rely on:

  • d_type: Type of entry:
    • DT_REG: File
    • DT_DIR: Directory
  • d_name: Name of the file or directory. Just the name, not the whole path.

This structure is reused on subsequent calls to readdir so if you need to save the values, you'll need to copy them.