

#include <sys/types.h> #include <dirent.h> void rewinddir(DIR *dir);
rewinddir positions a directory stream to the beginning.
dir is a pointer to an object associated with the open directory
by a call to opendir. After a call to rewinddir, the next
call to readdir reads the first entry in the directory. If the
contents of the directory have changed since the directory was opened,
rewinddir updates the directory stream for the next call to
readdir.
rewinddir returns 0 if it is successful
and - 1 if it is not successful.
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
main()
{
DIR *dir;
struct dirent *curfile;
int fd;
char flname[] = "testdir";
if ((dir=opendir(".")) == NULL)
perror("opendir() error");
else {
puts("Root directory contents:");
while ((curfile = readdir(dir)) != NULL)
/* Print the current directory contents. */
printf("n%s ", curfile->d_name);
/* Create test directory. */
if ((fd=mkdir(flname,S_IWUSR)) < 0)
perror("mkdir() error");
rewinddir(dir);
puts("nCurrent directory contents");
while ((curfile = readdir(dir)) != NULL)
printf("n%s ", curfile->d_name);
/* Remove testdir directory. */
if (rmdir(flname) != 0)
perror("rmdir error");
else
printf("nThe testdir directory %s has been removed.n",
flname);
}
closedir(dir);
}
opendir
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.