Next Previous Contents

3.157 readdir


Read a directory.




struct dirent* __fastcall__ readdir (DIR* dir);


readdir reads the next directory entry from the directory stream pointed to by dir. It stores the data in a dirent structure and returns a pointer to it. If the end of directory is reached, or an error occurs, NULL is returned. In case of errors, an error code is stored into errno.


  • The function is only available as fastcall function, so it may only be used in presence of a prototype.
  • The returned pointer may point to a statically allocated instance of struct dirent, so it may get overwritten by subsequent calls to readdir.
  • On several platforms, namely the CBMs and the Atari, the disk drives get confused when opening/closing files between directory reads. So for example a program that reads the list of files on a disk, and after each call to readdir, opens the file to process it, will fail.
    Possible solutions are reading the directory into memory before processing the file list, or to reset the directory by seeking to the correct position after opening/closing a file:
            seekdir (DIR, telldir (DIR));
    Platforms known to work without problems are: Apple.


POSIX 1003.1

See also

closedir, opendir



Next Previous Contents