post  Dirent.h

CD4 (25)   Link to this post
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
jsmith (3099)   Link to this post
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
CD4 (25)   Link to this post
can you please show me how exactly tis can be done.

thanks
Bazzy (3164)   Link to this post
Put line 10 in the loop and break the loop when drnt is false
CD4 (25)   Link to this post
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
Bazzy (3164)   Link to this post
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;
  //...
}
CD4 (25)   Link to this post
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
Bazzy (3164)   Link to this post
dirn will always be != NULL as it is an array. If the loops continues when drnt is equal to 0, when you attempt to access its members ( eg: drnt->d_name ) you will get the segmentation fault
CD4 (25)   Link to this post
bazzy.. u have any link to documents or code sample.As i follow your while (true) it shows error on compilation..
Bazzy (3164)   Link to this post
What does the error say?
Wikipedia has an example: http://en.wikipedia.org/wiki/Dirent.h#Example
chenzhizhong (1)   Link to this post
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;
}

Registered users can post in this forum.