gtpc2m0xC/C++ Language Support User's Guide

closedir-Close a Directory

This function closes a directory.

Format

#include <dirent.h>
int closedir(DIR *dir);

dir
The handle returned by opendir for the open directory that is to be closed.

This function closes the directory indicated by dir and frees the buffer that the readdir function uses when reading the directory stream.

Normal Return

If successful, the closedir function returns 0.

Error Return

If unsuccessful, the closedir function returns -1 and sets errno to the following:

EBADF
dir does not refer to an open directory stream.

Programming Considerations

None.

Examples

The following example closes a directory.

#include <dirent.h>
#include <sys/types.h>
#include <stdio.h>
 
main() {
  DIR *dir;
  struct dirent *entry;
  int count;
 
  if ((dir = opendir("/")) == NULL)
    perror("opendir() error");
  else {
    count = 0;
    while ((entry = readdir(dir)) != NULL) {
      printf("directory entry %03d: %s\n", ++count, entry->d_name);
    }
    closedir(dir);
  }
}

Output

directory entry 001: .
directory entry 002: ..
directory entry 003: dev
directory entry 004: etc
directory entry 005: lib
directory entry 006: tmp
directory entry 007: u
directory entry 008: usr

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.