Rewind a directory stream to the start of the directory
#include <sys/types.h> #include <dirent.h> void rewinddir( DIR * dirp );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The rewinddir() function rewinds the directory stream specified by dirp to the start of the directory. The directory stream will now refer to the current state of the directory, as if the calling thread had called opendir() again.
List all the files in a directory, create a new file, and then list the directory contents again:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <stdlib.h> int main( void ) { 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 ); } return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |