codescracker


c

C Program to List and Display Files in Directory



« Previous Program Next Program »


List and Display Files in Directory in C

To list and display all the files present inside the current directory in C programming, use findfirst() function as shown in the following program.

C Programming Code to List and Display Files in Current Directory

Following C program ask to the user to press any key to list all the files present inside the current directory. Here all the files displayed along with serial number like (1=FirstFileName.extension, 2=SecondFileName.extension, 3=ThirdFileName.extension, etc) and it will be separated by comma :

/* C Program - List and Display Files in Directory */
		
#include<stdio.h>
#include<conio.h>
#include<dir.h>
void main()
{
	clrscr();
	int done, i=1;
	struct ffblk a;
	printf("Press any key to list and view all the files in the current directory : \n");
	getch();
	done=findfirst("*.*", &a, 0);
	while(!done)
	{
		printf("%d=%s, ", i, a.ff_name);
		done=findnext(&a);
		i++;
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program list all files in directory

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test