Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering scandir for filenames in folder C Language

Tags:

c

scandir

I am using scandir() function in C, on a folder where i need to get files whose filenames are exactly = "exe".

How can i filter the entries returned by scandir?

Third argument of scandir is filter:

int scandir(const char *dirp, struct dirent ***namelist,
       int (*filter)(const struct dirent *),
       int (*compar)(const struct dirent **, const struct dirent **));

could it be useful for my purpose?

like image 581
Antonio Falcone Avatar asked Jul 12 '13 13:07

Antonio Falcone


1 Answers

Yes, the filter argument is a function pointer that lets you pass in a function to filter the results. You might want to write a function like the one below and pass it by name as the value for filter.

int file_select(const struct dirent *entry)
{
   return strcmp(entry->d_name, "exe");
}
like image 174
tuckermi Avatar answered Nov 14 '22 14:11

tuckermi