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.
closedir(), opendir(), readdir()
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
QNX