Directory Nodes [Archive] - C Board

PDA

View Full Version : Directory Nodes


Kinasz
10-22-2003, 02:27 AM
Does anybody know how to read contents of a directory into a c program?

eg: fopen( directory_name, "r"); ???

rotis23
10-22-2003, 03:27 AM
Have a look at stat(2) and readdir(3).

threahdead
10-22-2003, 10:24 AM
getdents(2)

Jaguar
10-22-2003, 10:36 AM
#include <dirent.h>
DIR *dp;
struct dirent *dirp;
dp=opendir("dirname");
while((dirp=readdir(dp))!=NULL)
printf("%s\n",dirp->d_name);
Bugs not fixed

threahdead
10-23-2003, 07:26 AM
what bug?

Jaguar
10-24-2003, 09:46 AM
>>what bug?

The program gave only concept.
It doesn't check the existence of ``dirname'' i.e.
if ((dp=opendir("dirname"))==NULL) {
/* do some stuff */
exit(1);
}