Re: folder parsing (newbie)problem



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.
.



Relevant Pages

  • Re: code optimiation
    ... Given that the compiler can often optimise the generated code to use the best sized types available, it's seldom worth specifying "fast" types explicitly. ... pointers and floating point types whose "zero value" might not be all- ... instruction, so the assembler produced for *p++ when used as the ... It will do the same job, and let you write the source code using proper array constructs. ...
    (comp.arch.embedded)
  • Re: Newbee needs once more again help with passing arrays out of a function
    ... what llong is etc. So in trying to write something useful ... if you want to pass back an array of integers and an array of ... int argc, char *argv) ... of char pointers we allocated before. ...
    (comp.lang.c)
  • Re: How to retrieve data from array of pointers or from a struct?
    ... You're declaring an array of pointers to unsigned long long, ... you're initializing the pointers with integer values. ... and your compiler should have warned ... You're not explicitly calling memcpy, ...
    (comp.lang.c)
  • Re: Returning a string array from a function
    ... function so that after calling the array I've got a list of strings ... void foo(char *sptr, int items) ... char buff; ... with pointers to pointers and malloc - neither of which I've used ...
    (comp.lang.c)
  • Re: Idea for ECMA/C# Standard - compile time hash for performance
    ... I agree with you the chance of a compiler change is slim, ... and then delegating to the standard hash for fields accessed less frequently. ... or the array lookup which would require the ... > 64-bit architecture) for each enum value that doesn't map to anything. ...
    (microsoft.public.dotnet.languages.csharp)