Dirent.h
CD4 (28)
Nov 3, 2009 at 4:05pm UTC
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 (5804)
Nov 3, 2009 at 5:56pm UTC
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 (28)
Nov 4, 2009 at 6:23pm UTC
can you please show me how exactly tis can be done.
thanks
Bazzy (6258)
Nov 4, 2009 at 7:56pm UTC
Put line 10 in the loop and break the loop when drnt is false
CD4 (28)
Nov 9, 2009 at 7:02am UTC
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 (6258)
Nov 9, 2009 at 3:09pm UTC
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 (28)
Nov 9, 2009 at 5:20pm UTC
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 (6258)
Nov 9, 2009 at 5:26pm UTC
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
CD4 (28)
Nov 10, 2009 at 1:14pm UTC
bazzy.. u have any link to documents or code sample.As i follow your while (true) it shows error on compilation..
chenzhizhong (1)
Nov 20, 2009 at 8:59am UTC
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.