You are here

C program to list files of a directory

C program to list all files present in a directory/folder in which executable file of this program is present. For example, if the executable file is present in C:\\TC\\BIN then it will list all the files present in C:\\TC\\BIN.

C programming code (Turbo C compiler only)

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <dir.h>
  4.  
  5. int main()
  6. {
  7.    int done;
  8.    struct ffblk a;
  9.  
  10.    printf("Press any key to view the files in the current directory\n");
  11.  
  12.    getch();
  13.  
  14.    done = findfirst("*.*", &a, 0); // The first '*' is for all file names and the second one is for all file extensions
  15.  
  16.    while(!done)
  17.    {
  18.       printf("%s\n", a.ff_name);
  19.       done = findnext(&a);
  20.    }
  21.  
  22.    getch();
  23.    return 0;
  24. }

Apparently, you will get a different output when you execute the program on your computer.