C Program to List Files in Directory
Posted by divya on December 05 2015 12:15:36
Here is source code of the C Program to list files in directory. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
* C Program to List Files in Directory
*/
#include <dirent.h>
#include <stdio.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);
}


$ cc pgm59.c
$ a.out
.
..
b.txt
pgm2.c
pgm5.c
1
a.out
a.txt
b.txt
pgm.c
pgm1.c
pgm10.c
pgm11.c
pgm12.c
pgm13.c
pgm14.c
pgm15.c
pgm16.c
pgm17.c
pgm18.c
pgm19.c
pgm2.c
pgm20.c
pgm21.c
pgm22.c
pgm23.c
pgm24.c
pgm25.c
pgm26.c
pgm27.c
pgm28.c
pgm29.c
pgm3.c
pgm30.c
pgm31.c
pgm32.c
pgm33.c
pgm34.c
pgm35.c
pgm36.c
pgm37.c
pgm38.c
pgm39.c
pgm4.c
pgm40.c
pgm41.c
pgm42.c
pgm43.c
pgm44.c
pgm45.c
pgm46.c
pgm47.c
pgm48.c
pgm49.c
pgm5.c
pgm50.c
pgm51.c
pgm52.c
pgm53.c
pgm54.c
pgm55.c
pgm56.c
pgm57.c
pgm58.c
pgm59.c
pgm6.c
pgm7.c
pgm8.c
pgm9.c