rewinddir() — Reposition a directory stream to the beginning

Standards

Standards / Extensions C or C++ Dependencies

POSIX.1
XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define_POSIX_SOURCE
#include <dirent.h>

void rewinddir(DIR *dir);

General description

Repositions an open directory stream to the beginning. dir points to a DIR object associated with an open directory.

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, a call to rewinddir() updates the directory stream so that a subsequent readdir() can read the new contents.

Returned value

rewinddir() returns no values.

Example

CELEBR15
⁄* CELEBR15

   This example produces the contents of a directory by opening it,
   rewinding it, and closing it.

 *⁄
#define _POSIX_SOURCE
#include <dirent.h>
#include <errno.h>
#include <sys⁄types.h>
#undef _POSIX_SOURCE
#include <stdio.h>

main() {
  DIR *dir;
  struct dirent *entry;

  if ((dir = opendir("⁄")) == NULL)
    perror("opendir() error");
  else {
    puts("contents of root:");
    while ((entry = readdir(dir)) != NULL)
      printf("%s ", entry->d_name);
    rewinddir(dir);
    puts("");
    while ((entry = readdir(dir)) != NULL)
      printf("%s ", entry->d_name);
    closedir(dir);
    puts("");
  }
}
Output
contents of root:
  . .. bin dev etc lib tmp u usr
  . .. bin dev etc lib tmp u usr

Related information