opendir
Opens a directory stream for reading.
Defined in header <dirent.h>
Syntax
#include <dirent.h>
DIR '*'opendir(char *dirname);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ |
Remarks
opendir is available on POSIX-compliant UNIX systems.
The opendir function opens a directory stream for reading. The name of the directory to read is dirname. The stream is set to read the first entry in the directory.
A directory stream is represented by the DIR structure, defined in dirent.h. This structure contains no user-accessible fields. More than one directory stream may be opened and read simultaneously. Directory entries can be created or deleted while a directory stream is being read.
Use the readdir function to read successive entries from a directory stream. Use the ciosedir function to remove a directory stream when it is no longer needed.
Return value
If successful, opendir returns a pointer to a directory stream that can be used in calls to readdir, rewinddir, and ciosedir. If the directory cannot be opened, opendir returns NULL and sets the global variable errno to
ENOENT The directory does not exist.
| ENOMEM | Not enough memory to allocate a DIR object. |
See also
Example
/* Using opendir, readdir, ciosedir */
ffinclude <dirent.h>
#include <stdio.h>
#include <stdlib.h>
void scandir(char *dirname)
{
DIR *dir;
struct dirent *ent;
printf ("First pass on '%s':\n",dirname) ;
if ((dir = opendir(dirname)) == NULL) {
perror ("Unable to open directory");
exit(l);
}
while ((ent =.readdir(dir)) != NULL)
printf ("%s\n",ent->d_name);
printf ("Second pass on '%s':\n",dirname) ,•
rewinddir (dir);
while ((ent = readdir(dir)) != NULL)
printf ("%s\n",ent->d_naine) ;
if (ciosedir(dir) != 0)
perror ("Unable to close directory");
}
void main (int argc,char *argv[])
{
if (argc != 2) {
printf ("usage: opendir dirname\n");
exit(l);
}
scandir(argv[l] );
exit(O);
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/dirent.h/opendir/
· short form /3.1/opendir/