readdir

Reads the directory entries of a directory.

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <dirent.h> 
   
  DIR *readdir (
     DIR  *dirp);
  

Parameters

dirp

(IN/OUT) Specifies the structure to receive information about the next directory entry. See DIR.

Return Values

If successful, returns a pointer to a DIR object structure containing information about the next directory entry. When the end of the directory is encountered, NULL is returned and errno is not changed.

If an error occurs, NULL is returned and errno is set to indicate one of the following errors.

Decimal

Constant

Description

1

ENOENT

The current position of the directory stream is invalid.

4

EBADF

The dirp argument does not refer to an open directory stream.

81

EOVERFLOW

One of the values in the structure to be returned cannot be represented correctly.

Remarks

The readdir function can be called repeatedly to obtain the list of file and directory names contained in the directory specified by the pathname given to the opendir function.

The closedir function must be called to close the directory and free the memory allocated by the opendir function.

The readdir function does not return entries containing empty names. If entries for dot or dot-dot exist, one entry is returned for dot and one entry is returned for dot-dot; otherwise, they are not returned.

The pointer returned by readdir points to data, which may be overwritten by another call to readdir on the same directory stream. This data is not overwritten by another call to readdir on a different directory stream.

If a file is removed or added to the directory after the most recent call to opendir or rewinddir, a subsequent call to readdir might or might not return an entry for the file; the behavior is unspecified.

The readdir function marks for update the st_atime field of the directory each time the directory is actually read.

See Also