closedir

Closes directories.

Format

#include  <dirent.h>

int closedir  (DIR *dir_pointer);

Argument

dir_pointer
Pointer to the dir structure of an open directory.

Description

This function closes a directory stream and frees the structure associated with the dir_pointer argument. Upon return, the value of dir_pointer does not necessarily point to an accessible object of the type DIR.

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


Note
An open directory must always be closed with the closedir function to ensure that the next attempt to open the directory is successful.

Example

    The following example shows how to search a directory for the entry name, using the opendir, readdir, and closedir functions:

    len = strlen(name);
    dir_pointer = opendir(".");
        for (dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer))
            if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
       closedir(dir_pointer);
       return FOUND;
           }
       closedir(dir_pointer);
       return NOT_FOUND;
    

Return Values
Indicates success. 
-1  Indicates an error and is further specified in the global errno. 


Previous Page | Next Page | Table of Contents | Index