C
collinm
hi
i expect to find 7 file on a folder... maybe less, maybe more...
i can surely put this number to 1... but i think doing some realloc
will be expensive...
after 7 files, i need to use realoc...
somebody could help me to use realloc?
thanks
i expect to find 7 file on a folder... maybe less, maybe more...
i can surely put this number to 1... but i think doing some realloc
will be expensive...
after 7 files, i need to use realoc...
somebody could help me to use realloc?
Code:
int searchfile(char *dir, char *filename, int flength, char *ext)
{
DIR *pdir;
printf("dir: %s\n",dir);
pdir = opendir(dir);
struct dirent *pent;
char **fileArray;
int lenFile=20;
int fileCount=0;
int expectedFile=7;
fileArray = (char**) malloc( expectedFile * sizeof(char*) );
if (!pdir)
{
printf ("fct: searchfile - %s - Incapable d'utiliser
opendir()\n", strerror (errno));
return 1;
}
snprintf(tmp_mnt_dir_www, sizeof(tmp_mnt_dir_www),"%s/%s",
mnt_dir_www, "ihist.htm");
if((fp = fopen(tmp_mnt_dir_www, "w"))!=NULL)
{
while((pent = readdir(pdir)) != NULL)
{
if(strncmp(pent->d_name, filename, 5) == 0 &&
strcmp(pent->d_name + flength, ext) == 0)
{
if(expectedFile<7)
{
fileArray[fileCount] = (char*) malloc( (lenFile+1)
* sizeof(char) );
}
else
fileArray = realloc( (char
*)fileArray,(lenFile+1)*(sizeof (char);
fileCount++;
strcpy( fileArray[fileCount], pent->d_name );
}
}
;
fclose(fp);
}
for(i=0;i<fileCount;i++);
free(fileArray[i]);
free( fileArray );
closedir(pdir);
return 0;
}
thanks