Dirent.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>

struct dirent *drnt;
int main() {
DIR *dr;
dr=opendir("/usr");
drnt=readdir(dr);
 //while (drnt->d_name!=NULL){
 printf("%s",drnt->d_name);
//}
 printf("%s","no more DIR");
closedir(dr);
return 0;
}

hello , i wish to know that when i debug i get all the directories in the d_name. but only libexe is shown... why so??

regards
readdir() only returns one directory entry at a time.

You have to keep calling readdir() until it returns 0, which indicates that there are no more entries.

man 2 readdir
can you please show me how exactly tis can be done.

thanks
Put line 10 in the loop and break the loop when drnt is false
Hi, finally i achieved how to use readdir().But after that i went on with experimentation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<string.h>

struct dirent *drnt;
struct stat buffr;
int main() {
char dirn[50];
DIR *dir;
gets(dirn);
dir=opendir(dirn);
printf("%s\n","output:");
while (dirn!=NULL) {
char name[50]="/";
drnt=readdir(dir);
printf("%s\n",drnt->d_name);
strcat(name,drnt->d_name);
stat(name,&buffr);
printf("%d\n",(int)buffr.st_size);
name[50]=0;
}
printf("%s","No,directory at all\n");
closedir(dir);
return 0;
}

Well its listing me all the directories and also the sizes but i get a segmantation fault in the end.. Can someone point out mistake.


regards
The while checking is wrong. This is what you should have=
1
2
3
4
5
6
7
8
while (true) 
{
  char name[50]="/";
  drnt=readdir(dir);
  if ( !drnt ) 
    break;
  //...
}
bazzy ..do u think this is causing the segmentation fault.?? i get all the directories listed but finally a segmentation fault..can you explain.. i am sorry if i have asked stupid question.. i am newbie..

regards
dirn will always be != NULL as it is an array. If the loops continues when drnt is equal to , when you attempt to access its members ( eg: drnt->d_name ) you will get the segmentation fault
bazzy.. u have any link to documents or code sample.As i follow your while (true) it shows error on compilation..
What does the error say?
Wikipedia has an example: http://en.wikipedia.org/wiki/Dirent.h#Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<string.h>

int main()
{
        char dirn[50];
        DIR *dir = NULL;
        struct dirent *drnt = NULL;

        printf("Input dir name: ");
        gets(dirn);

        dir=opendir(dirn);
        if(dir)
        {
                printf("output:\n");
                while(drnt = readdir(dir))
                {
                        printf("%-20s\t", drnt->d_name);
                }
                closedir(dir);
        }
        else
        {
                printf("Can not open directory '%s'\n", dirn);
        }

        return 0;
}
Topic archived. No new replies allowed.