O
oswald.harry
hi
i am learning to parse directory using dirent.h (using gcc of cygwin
as compiler)and tried to make an array of names of .png files in a
directory
here my problem is with accessing a char* []
I wrote this function
int parsedirectory(char * dirname){
struct dirent **filelist = {0};
char * directory =dirname;
int fcount = -1;
int i = 0;
fcount = scandir(directory, &filelist, 0, alphasort);
if(fcount < 0) {
perror(directory);
return 1;
}
int pngcnt;
pngcnt=0;
for(i = 0; i < fcount; i++) {
//i want to check if filename has a .png in it
if(strstr(filelist->d_name,".png")!=NULL){
++jpgcnt;
}
}
printf("png imgs:%d\n",pngcnt);//this shows totalnumber of .pngs
//then I can create a filenames array of size pngcnt
char* filenames[pngcnt];
//then i want to strcat the foldername with the imagename and add it
to the array
//so each entry in the array will be a string like "F:\code\c\testparse
\man1.png"
//but here i am stuck since the value fcount can't be used to //index
this array
//i tried like this
char fldr[strlen(dirname)];
strcpy(fldr,dirname);
strcat(fldr,"\\");
int j;//this i use to index into filenames[]
j=0;
for(i=0;i<fcount;i++){
if(strstr(filelist->d_name,".png")!=NULL){
char* name; //to make full name of a file
char fldrtemp[strlen(fldr)];
strcpy(fldrtemp,fldr);//so that strcat will not chang fldr
name=strcat(fldrtemp,filelist->d_name);
pngfilenames[j++]=name;
}
}
}
my problem is that when i try to iterate thru pngfilenames using
for(i=0;i<pngcnt;i++){
printf("%s\n",pngfilenames);
}
i get something like
D"
D"
D"
D"
not full path imagefilenames as expected!!
i am not sure if this is the right way..my c learning is in its
infancy so if
anyone can suggest a right way i will be grateful
i am learning to parse directory using dirent.h (using gcc of cygwin
as compiler)and tried to make an array of names of .png files in a
directory
here my problem is with accessing a char* []
I wrote this function
int parsedirectory(char * dirname){
struct dirent **filelist = {0};
char * directory =dirname;
int fcount = -1;
int i = 0;
fcount = scandir(directory, &filelist, 0, alphasort);
if(fcount < 0) {
perror(directory);
return 1;
}
int pngcnt;
pngcnt=0;
for(i = 0; i < fcount; i++) {
//i want to check if filename has a .png in it
if(strstr(filelist->d_name,".png")!=NULL){
++jpgcnt;
}
}
printf("png imgs:%d\n",pngcnt);//this shows totalnumber of .pngs
//then I can create a filenames array of size pngcnt
char* filenames[pngcnt];
//then i want to strcat the foldername with the imagename and add it
to the array
//so each entry in the array will be a string like "F:\code\c\testparse
\man1.png"
//but here i am stuck since the value fcount can't be used to //index
this array
//i tried like this
char fldr[strlen(dirname)];
strcpy(fldr,dirname);
strcat(fldr,"\\");
int j;//this i use to index into filenames[]
j=0;
for(i=0;i<fcount;i++){
if(strstr(filelist->d_name,".png")!=NULL){
char* name; //to make full name of a file
char fldrtemp[strlen(fldr)];
strcpy(fldrtemp,fldr);//so that strcat will not chang fldr
name=strcat(fldrtemp,filelist->d_name);
pngfilenames[j++]=name;
}
}
}
my problem is that when i try to iterate thru pngfilenames using
for(i=0;i<pngcnt;i++){
printf("%s\n",pngfilenames);
}
i get something like
D"
D"
D"
D"
not full path imagefilenames as expected!!
i am not sure if this is the right way..my c learning is in its
infancy so if
anyone can suggest a right way i will be grateful