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.
#include<stdio.h>
#include<dirent.h>
int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
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.