Chapter Contents

Previous

Next
rewinddir

rewinddir



Rewind Directory Stream

Portability: POSIX.1 conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <sys/types.h>
#include <dirent.h>

void rewinddir(DIR *dir);


DESCRIPTION

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 .


RETURN VALUE

rewinddir returns 0 if it is successful and -1 if it is not successful.


EXAMPLE

#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);
}


RELATED FUNCTIONS

opendir


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.