#include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp);
The closedir() function closes the directory stream indicated by dirp. It frees the buffer that readdir() uses when reading the directory stream.
A file descriptor is used for type DIR; closedir() closes the file descriptor.
No authorization is required. Authorization is verified during opendir().
If closedir() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EACCES] |
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EAGAIN] | |
[EBADF] | |
[EBADFID] | |
[EBUSY] | |
[EDAMAGE] | |
[EFAULT] | |
[EINTR] | |
[EINVAL] | |
[EIO] | |
[EJRNDAMAGE] | |
[EJRNENTTOOLONG] | |
[EJRNINACTIVE] | |
[EJRNRCVSPC] | |
[ENEWJRN] | |
[ENEWJRNRCV] | |
[ENOSPC] | |
[ENOSYS] | |
[ENOTAVAIL] | |
[ENOTSAFE] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[EUNKNOWN] |
If interaction with a file server is required to access the object, errno could indicate one of the following errors:
Error condition | Additional information |
---|---|
[EADDRNOTAVAIL] | |
[ECONNABORTED] | |
[ECONNREFUSED] | |
[ECONNRESET] | |
[EHOSTDOWN] | |
[EHOSTUNREACH] | |
[ENETDOWN] | |
[ENETRESET] | |
[ENETUNREACH] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[ETIMEDOUT] | |
[EUNATCH] |
The following messages may be sent from this function:
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
See Code disclaimer information for information pertaining to code examples.
The following example closes a directory:
#include <stdio.h> #include <sys/types.h> #include <dirent.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: QSYS.LIB directory entry 004: QDLS directory entry 005: QOpenSys directory entry 006: home
Top | UNIX-Type APIs | APIs by category |