C Program to List Files in Directory

This C Program lists files in directory.

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.

  1. /*
  2.  * C Program to List Files in Directory
  3.  */
  4. #include <dirent.h>
  5. #include <stdio.h>
  6.  
  7. int main(void)
  8. {
  9.     DIR *d;
  10.     struct dirent *dir;
  11.     d = opendir(".");
  12.     if (d)
  13.     {
  14.         while ((dir = readdir(d)) != NULL)
  15.         {
  16.             printf("%s\n", dir->d_name);
  17.         }
  18.         closedir(d);
  19.     }
  20.     return(0);
  21. }

$ 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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on File Handling, go to File Handling. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.