Open a directory
#include <dirent.h> DIR * opendir( const char * dirname );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The opendir() function is used with readdir() and closedir() to get the list of file names contained in the directory specified by dirname.
You can read more than one directory at the same time using the opendir(), readdir(), rewinddir() and closedir() functions.
A pointer to a DIR structure required for subsequent calls to readdir() to retrieve the file names in dirname, or NULL if dirname isn't a valid path (errno is set).
Get a list of files contained in the directory /home/fred:
#include <stdio.h> #include <stdlib.h> #include <dirent.h> int main( void ) { DIR* dirp; struct dirent* direntp; dirp = opendir( "/home/fred" ); if( dirp == NULL ) { perror( "can't open /home/fred" ); } else { 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 | No |
Thread | Yes |