在文件夹C语言中筛选文件名的scandir(Filtering scandir for filenames in folder C Language)

编程入门 行业动态 更新时间:2024-10-28 11:30:06
在文件夹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"); }

更多推荐

本文发布于:2023-07-24 20:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件名   文件夹   语言   scandir   Language

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!