This example C program demonstrates how to read the files in the current directory.
#include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <string.h> #include <errno.h> int main () { DIR * d; char * dir_name = "."; /* Open the current directory. */ d = opendir (dir_name); if (! d) { fprintf (stderr, "Cannot open directory '%s': %s\n", dir_name, strerror (errno)); exit (EXIT_FAILURE); } while (1) { struct dirent * entry; entry = readdir (d); if (! entry) { break; } printf ("%s\n", entry->d_name); } /* Close the directory. */ if (closedir (d)) { fprintf (stderr, "Could not close '%s': %s\n", dir_name, strerror (errno)); exit (EXIT_FAILURE); } return 0; }
The output of the example looks like this:
. .. tags index.html makefile links-table ld.c ld ld.c.~1~ ld.txt