Re: folder parsing (newbie)problem
- From: Nick Keighley <nick_keighley_nospam@xxxxxxxxxxx>
- Date: Tue, 22 Jan 2008 07:02:20 -0800 (PST)
On 22 Jan, 08:13, oswald.ha...@xxxxxxxxx wrote:
i am learning to parse directory using dirent.h (using gcc of cygwin
as compiler)
technically direct.h is off-topic. If your problem is with
dirent.h you need to take it to a unix news group.
But I'm not sure your problem is dirent.h...
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};
you've initialised this as if it were a struct.
It's not it's a ptr.
use
struct dirent **filelist = NULL;
char * directory =dirname;
int fcount = -1;
int i = 0;
fcount = scandir(directory, &filelist, 0, alphasort);
if(fcount < 0) {
perror(directory);
return 1;
}
int pngcnt;
you can't mix declarations with statements in C89
(the most portable standard).
pngcnt=0;
for(i = 0; i < fcount; i++) {
//i want to check if filename has a .png in it
if(strstr(filelist[i]->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];
this is an array of char*. Somewhere you must allocate some memory
for these pointers to point to. You don't.
//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"
beware of // comments. They screw up in postings
//but here i am stuck since the value fcount can't be used to
//index this array
I've no idea what this means.
//i tried like this
char fldr[strlen(dirname)];
you can't use strlen() as an array dimension. Does this
even compile? If so you are using a weird compiler.
(maybe gcc not in ANSI mode). Assume dirname is six characters.
Then strlen is 6.
strcpy(fldr,dirname);
oops! dirname actually occupies *7* bytes (one for the nul
char).
strcat(fldr,"\\");
oops!! you now have 8 bytes in your 6 byte array.
Use malloc()
char *fldr = malloc (strlen(dirname) + 2);
if (fldr == 0) abort();
int j; //this I use to index into filenames[]
j=0;
for(i=0;i<fcount;i++){
if(strstr(filelist[i]->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[i]->d_name);
more of the same. Also you point name at fldrtemp[]
which disappears at the end of the for loop.
And then probably gets reused on the next iteration.
Use malloc().
char* name = malloc (strlen(fldr) + strlen(filelist[i]->d_name) +
1);
strcpy (fldrtemp, fldr);
name = strcat (fldrtemp,filelist[i]->d_name);
/* or replace the last two lines with
sprintf (name, "%s%s", fldr, filelist[i]->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]);
}
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
a general suggestion is break the program down into
smaller steps. Write a program then returns all the files
in the directory. Print them out. Does that look ok?
Filter out the pngs. Print them
<OT>
you do know scandir() will do this for you?
</OT>
turn up your compiler warnings. If it's gcc
use
gcc -W -Wall -ansi -pedantic
and maybe -O2
you should probably read up on pointers and on malloc().
char *name[23];
defines an array of pointers. It *does not* allocate
any memory for the pointers to point at.
--
Nick Keighley
Quality: this activity has been delayed due to higher priority tasks.
.
- References:
- folder parsing (newbie)problem
- From: oswald . harry
- folder parsing (newbie)problem
- Prev by Date: Re: using 32bit glibc/libgcc on a 64bit machine
- Next by Date: Re: const qualifier
- Previous by thread: Re: folder parsing (newbie)problem
- Next by thread: Wholesale all NIKE shoes.all models of nokia .Apple Ipods Nano, Xbox 360, Sony PS3, Sony PSP.wii.all laptops http://www.new-nikeshoes.cn
- Index(es):
Relevant Pages
|