reset the position of a directory stream to the start of the directory
#include <sys/types.h> #include <dirent.h> void rewinddir( DIR *dirp );
The rewinddir() function resets the position of the directory stream to which dirp refers to the beginning of the directory. It also causes the directory stream to refer to the current state of the corresponding directory, as a call to opendir() would have done.
The result of using a directory stream after one of the exec... or spawn... family of functions is undefined. After a call to the fork() function, either the parent or the child (but not both) may continue processing the directory stream, using readdir() or rewinddir(), or both. If both the parent and child processes use these functions, the result is undefined. Either or both processes may use closedir(). |
The following example lists all the files in a directory, creates a new file, and then relists the directory:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> void main() { DIR *dirp; struct dirent *direntp; int filedes ; dirp = opendir( "/home/fred" ); if( dirp != NULL ) { printf( "Old directory listing\n" ); for(;;) { direntp = readdir( dirp ); if( direntp == NULL ) break; printf( "%s\n", direntp->d_name ); } filedes = creat( "/home/fred/file.new", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); close( filedes ); rewinddir( dirp ); printf( "New directory listing\n" ); for(;;) { direntp = readdir( dirp ); if( direntp == NULL ) break; printf( "%s\n", direntp->d_name ); } closedir( dirp ); } }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes |
Thread | No |
closedir(), opendir(), readdir()