C Program To List Contents of Directory and Sub Directories
Learn How To List Names of all Files, Sub Directories and Contents of Directory in C Programming Language. Get the list of all files and sub directories in a directory using dirent.h header file.
This is a simple directory lister c program that enlists the names of all the files in a directory. We have used opendir() method that passes the directory name to the pointer which is then used by readdir() method to return the names of the files and sub directories in that directory.
The dirent.h header file is used to work with the directories in C programming. DIR is a type representing a directory stream. The struct dirent structure is a structure type used to return information about directory entries.
C Program To List Contents of Directory using dirent.h
struct dirent *directory;
printf("Enter Directory Name:\t");
ptr = opendir(directory_name);
printf("\nDirectory %s\n", directory_name);
while((directory = readdir(ptr)) != NULL)
If you have any compilation error or doubts in this C program to list all the sub directories and folders in a given directory, let us know about it in the comment section below. Find more about dirent.h here.