Signup/Sign In

C Program to Print names of all Files present in a Directory

dirent.h header file contains variables and functions related to directory streams.

Below is a program to print the names of all the files in a directory.


int main(void)
    DIR *d;
    struct dirent *dir;
    d = opendir(".");
    if (d)
        while ((dir = readdir(d)) != NULL)
            printf("%s\n", dir->d_name);

File1.txt File2.txt File3.txt File4.txt File5.txt File6.txt File7.txt

We can also take the directory name as input from the user, and can also create a simple C program to search for a particular file in a directory.