过滤文件夹C语言中文件名的scandir

Ant*_*one 5 c scandir

我在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 **));
Run Code Online (Sandbox Code Playgroud)

它对我的目的有用吗?

tuc*_*rmi 9

是的,filter参数是一个函数指针,允许您传入一个函数来过滤结果.您可能希望编写类似下面的函数,并按名称将其作为filter的值传递.

int file_select(const struct dirent *entry)
{
   return strcmp(entry->d_name, "exe");
}
Run Code Online (Sandbox Code Playgroud)

  • 你的回报不对!应该是 `return (0 == strcmp(entry->d_name, "exe");` (3认同)