readdir

Finds entries in a directory.

Format

#include  <dirent.h>

struct dirent *readdir  (DIR *dir_pointer);

Arguments

dir_pointer
A pointer to the dir structure of an open directory.

Description

This function returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by dir_pointer, and positions the directory stream at the next entry. It returns a NULL pointer upon reaching the end of the directory stream. The dirent structure defined in the <dirent.h> header file describes a directory entry.

The type DIR defined in the <dirent.h> header file represents a directory stream. A directory stream is an ordered sequence of all the directory entries in a particular directory. Directory entries represent files. You can remove from or add files to a directory asynchronously to the operation of the readdir function.

The pointer returned by the readdir function points to data that you can overwrite 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 from or added to the directory after the most recent call to the opendir or rewinddir function, whether a subsequent call to the readdir function returns an entry for that file, is unspecified.

When it reaches the end of the directory, or when it detects an invalid seekdir operation, the readdir function returns the null value.

An attempt to seek to an invalid location causes the readdir function to return the null value the next time it is called. A previous telldir function call returns the position.

See also opendir, rewinddir, seekdir, and telldir in this section.

Example

    See the description of closedir for an example.

Return Values
A pointer to an object of type struct dirent. 
NULL  Indicates an error, returns a pointer. The global errno indicates the error. When the end of the directory is encountered, errno is not changed. 


Previous Page | Next Page | Table of Contents | Index