For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > January 2008 > newbie with dirent.h









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author newbie with dirent.h
oswald.harry@gmail.com

2008-01-23, 4:29 am

hi
am a beginner in c and uses gcc of cygwin on WinXP..
i was following some sample code to parse directories using dirent.h
and list the files

int parsedirectory(char * dirname){
struct dirent **filelist = NULL;
char * directory =dirname;
int jpggcnt=0;/* num of jpeg files in directory*/
int fcount = -1;
int i = 0;
fcount = scandir(directory, &filelist, 0, alphasort);
if(fcount < 0) {
perror(directory);
return 1;
}
printf("fcount:%d\n",fcount);
for(i = 0; i < fcount; i++) {
printf("%02d: %s\n", i, filelist[i]->d_name);
if((strstr(filelist[i]->d_name,".jpg")!=NULL)||(strstr(filelist[i]-
>d_name,".jpeg")!=NULL)){

++jpgcnt;
}
free(filelist[i]);
}
printf("total jpeg imgs:%d\n",jpgcnt);
free(filelist);
return 0;
}

when printf("%02d: %s\n", i, filelist[i]->d_name) executes it gives
the filenames but the first 2 printed names are like
00: .
01: ..
what does that mean? also the fcount are 2 more than rhe number of
files in the directory
also why do i need
free(filelist[i]) and free(filelist) ?

oharry
Ben Bacarisse

2008-01-23, 8:20 am

oswald.harry@gmail.com writes:

> int parsedirectory(char * dirname){
> struct dirent **filelist = NULL;
> char * directory =dirname;
> int jpggcnt=0;/* num of jpeg files in directory*/
> int fcount = -1;
> int i = 0;
> fcount = scandir(directory, &filelist, 0, alphasort);
> if(fcount < 0) {
> perror(directory);
> return 1;
> }
> printf("fcount:%d\n",fcount);
> for(i = 0; i < fcount; i++) {
> printf("%02d: %s\n", i, filelist[i]->d_name);
> if((strstr(filelist[i]->d_name,".jpg")!=NULL)||(strstr(filelist[i]-

If all you want is the .jpeg files, you can use the filter function
(3rd parameter of scandir) to get only these.
[color=darkred]
> ++jpgcnt;
> }
> free(filelist[i]);
> }
> printf("total jpeg imgs:%d\n",jpgcnt);
> free(filelist);
> return 0;
> }
>
> when printf("%02d: %s\n", i, filelist[i]->d_name) executes it gives
> the filenames but the first 2 printed names are like
> 00: .
> 01: ..


These entries are links to the current directory and to the parent
directory respectively. You simply ignore them.

> what does that mean? also the fcount are 2 more than rhe number of
> files in the directory


Because . and .. are considered part of the directory. They are
included in the count.

> also why do i need
> free(filelist[i]) and free(filelist) ?


Because the manual tells you that the storage for the whole list and
for each entry is allocated using malloc. You don't have to free the
storage at that point -- you can keep the data for as long as you
like -- but it should be freed eventually.

--
Ben.
oswald.harry@gmail.com

2008-01-24, 7:24 pm



>
> If all you want is the .jpeg files, you can use the filter function
> (3rd parameter of scandir) to get only these.
>


thanx for the reply..ummm about the filter function..do i have to
write another function and pass it as the third arg here..?

oharry
Ben Bacarisse

2008-01-24, 7:24 pm

oswald.harry@gmail.com writes:

>
> thanx for the reply..ummm about the filter function..do i have to
> write another function and pass it as the third arg here..?


Yes but it is one or two lines long with a call to strstr. I'd also
check that, having found ".jpeg" (or whatever), it is followed by
'\0'. Otherwise you will find files that don't end with .jpeg.

--
Ben.
oswald.harry@gmail.com

2008-01-25, 4:33 am


> Yes but it is one or two lines long with a call to strstr. I'd also
> check that, having found ".jpeg" (or whatever), it is followed by
> '\0'. Otherwise you will find files that don't end with .jpeg.
>



Ben
thanx for the help..i wrote the filter function as you suggested ..it
works..but i think it is a bit bloated..since i am a beginner with c
and programming ,if you can suggest how i can improve this it wd be
great help
thanx
oharry

int selectjpeg(const struct dirent * a){
char* ptrtojpegpart=NULL;
if( ( (ptrtojpegpart=strstr(a->d_name,".jpg") ) !=NULL) ||
( (ptrtojpegpart=strstr(a->d_name,".jpeg") ) !=NULL))
{
while(*ptrtojpegpart !='\0'){
ptrtojpegpart++;
if(*ptrtojpegpart =='g'){
ptrtojpegpart++;
if(*ptrtojpegpart =='\0'){
return 1;
}
else return 0;
}
}
}

else
return 0;
}

int parsedirectory(char * dirname){
struct dirent **filelist = NULL;
char * directory =dirname;

int fcount = -1;
int i = 0;
fcount = scandir(directory, &filelist, selectjpeg, alphasort);
if(fcount < 0) {
perror(directory);
return -1;
}
printf("total jpeg imgs:%d\n",fcount);
return fcount;
}
Ben Bacarisse

2008-01-25, 8:19 am

oswald.harry@gmail.com writes:

>
>
> Ben
> thanx for the help..i wrote the filter function as you suggested ..it
> works..but i think it is a bit bloated..since i am a beginner with c
> and programming ,if you can suggest how i can improve this it wd be
> great help


Since you want to test for various endings, I'd write it like this
(warning: not even compiled let alone tested!):

int ends_with(const char *haystack, const char *needle)
{
const char *found = strcasestr(haystack, needle);
return found && found[strlen(needle)] == 0;
}

int selectjpeg(const struct dirent *d)
{
return ends_with(d->d_name, ".jpg") || ends_with(d->d_name, ".jpeg");
}

If you don't have strcasecmp (compile with _GNU_SOURCE defined) you
might have to write it.

<code snipped>

--
Ben.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com