在文件夹C语言中筛选文件名的scandir(Filtering scandir for filenames in folder C Language)
我在C中使用scandir()函数,在我需要获取文件名正好是“exe”的文件的文件夹中。
我如何筛选scandir返回的条目?
scandir的第三个参数是filter:
int scandir(const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **));它对我的目的有用吗?
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?
最满意答案
是的,过滤器参数是一个函数指针,可以让你传入一个函数来过滤结果。 您可能希望编写一个如下所示的函数,并按名称将其作为过滤器的值传递。
int file_select(const struct dirent *entry) { return strcmp(entry->d_name, "exe"); }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"); }更多推荐
发布评论