opendir

Opens a specified directory.

Format

#include  <dirent.h>

DIR *opendir  (const char *dir_name);

Arguments

dir_name
The name of the directory to be opened.

Description

This function opens the directory specifed by dir_ name and associates a directory stream with it. The directory stream is positioned at the first 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.

The opendir function also returns a pointer to identify the directory stream in subsequent operations. The NULL pointer is returned when the directory named by dir_name cannot be accessed, or when not enough memory is available to hold the entire stream.


Note
An open directory must always be closed with the closedir function to ensure that the next attempt to open that directory is successful. The opendir function should be used with readdir, closedir, and rewinddir to examine the contents of the directory.

Example

    See closedir for an example.

Return Values
A pointer to an object of type DIR. 
NULL  Indicates an error; errno is set to one of the following values:

  • EACCES - Search permission is denied for any component of dir_name or read permission is denied for dir_ name.

  • ENAMETOOLONG - The length of the dir_name string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX.

  • ENOENT - The dir_name argument points to the name of a file that does not exist, or is an empty string.
 


Previous Page | Next Page | Table of Contents | Index