Does anybody know how to read contents of a directory into a c program?
eg: fopen( directory_name, "r"); ???
Printable View
Does anybody know how to read contents of a directory into a c program?
eg: fopen( directory_name, "r"); ???
Have a look at stat(2) and readdir(3).
getdents(2)
Bugs not fixedCode:#include <dirent.h>
DIR *dp;
struct dirent *dirp;
dp=opendir("dirname");
while((dirp=readdir(dp))!=NULL)
printf("%s\n",dirp->d_name);
what bug?
>>what bug?
The program gave only concept.
It doesn't check the existence of ``dirname'' i.e.
Code:if ((dp=opendir("dirname"))==NULL) {
/* do some stuff */
exit(1);
}