Lately, I have been trying to get Current Directory List for one of my program in C. So, thought of sharing this with you all.
Source Code
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include<conio.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
printf("%s \n", ep->d_name);
getch();
(void) closedir (dp);
}
else
printf ("Couldn't open the directory");
return 0;
}
This code has been taken from gnu.org
Data Type: struct dirent
This is a structure type used to return information about directory entries.
Data Type: DIR
The
You shouldn't ever allocate objects of the
Functions Used Under <dirent.h>
This function reads the next entry from the directory. It normally returns a pointer to a structure containing information about the file. This structure is statically allocated and can be rewritten by a subsequent call.
This function closes the directory stream dirstream. It returns
If unsuccessful,
Source Code
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include<conio.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
printf("%s \n", ep->d_name);
getch();
(void) closedir (dp);
}
else
printf ("Couldn't open the directory");
return 0;
}
This code has been taken from gnu.org
Output
Data Type: struct dirent
This is a structure type used to return information about directory entries.
Data Type: DIR
The
DIR data type represents a directory stream.
You shouldn't ever allocate objects of the
struct dirent or
DIR data types, since the directory access functions do that for
you.Functions Used Under <dirent.h>
This function reads the next entry from the directory. It normally returns a pointer to a structure containing information about the file. This structure is statically allocated and can be rewritten by a subsequent call.
This function closes the directory stream dirstream. It returns
0 on success and -1 on failure.- DIR * opendir (const char *dirname)
opendir function opens and returns a directory stream for
reading the directory whose file name is dirname. The stream has
type DIR *.
If unsuccessful,
opendir returns a null pointer.