#### 14.3.5 Random Access in a Directory Stream ¶

This section describes how to reread parts of a directory that you have already read from an open directory stream. All the symbols are declared in the header file `dirent.h`.

Function: `void` rewinddir `(DIR *dirstream)` ¶

  Preliminary: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock | See POSIX Safety Concepts.

  The `rewinddir` function is used to reinitialize the directory stream dirstream, so that if you call `readdir` it returns information about the first entry in the directory again. This function also notices if files have been added or removed to the directory since it was opened with `opendir`. (Entries for these files might or might not be returned by `readdir` if they were added or removed since you last called `opendir` or `rewinddir`.)

  For example, it is recommended to call `rewinddir` followed by `readdir` to check if a directory is empty after listing it with `readdir` and deleting all encountered files from it.

Function: `long int` telldir `(DIR *dirstream)` ¶

  Preliminary: | MT-Safe | AS-Unsafe heap/bsd lock/bsd | AC-Unsafe mem/bsd lock/bsd | See POSIX Safety Concepts.

  The `telldir` function returns the file position of the directory stream dirstream. You can use this value with `seekdir` to restore the directory stream to that position.

  Using the the `telldir` function is not recommended.

  The value returned by `telldir` may not be compatible with the `d_off` field in `struct dirent`, and cannot be used with the `lseek` function. The returned value may not unambiguously identify the position in the directory stream.

Function: `void` seekdir `(DIR *dirstream, long int pos)` ¶

  Preliminary: | MT-Safe | AS-Unsafe heap/bsd lock/bsd | AC-Unsafe mem/bsd lock/bsd | See POSIX Safety Concepts.

  The `seekdir` function sets the file position of the directory stream dirstream to pos. The value pos must be the result of a previous call to `telldir` on this particular stream; closing and reopening the directory can invalidate values returned by `telldir`.

  Using the the `seekdir` function is not recommended. To seek to the beginning of the directory stream, use `rewinddir`.