读取指定文件夹内所有文件列表

今天分析下利用 scandir 函数获取文件列表。

今天分析下利用 scandir 函数获取文件列表。

函数原型

#include <dirent.h>

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

scandir() 会扫描目录 dirp(不包括子目录),经由参数 filter 指定的函数来挑选符合条件的目录结构至参数namelist 数组中,最后再调用参数 compar 指定的函数来排序 namelist 数组中的目录数据。

每次从 dirp 中读取的目录结构后都会传递给 filter 进行过滤,若 filter 返回 0 则不会把该目录结构复制到 namelist 数组中。

若 filter 参数为 NULL,则选择所有目录到 namelist 组中。

scandir() 中会调用 qsort() 来对获取的目录列表进行排序,参数 compar 则为 qsort() 的参数,若是要把目录名称列表按照字母顺序排序则 compar 参数可使用 alphasort()。

返回值 : 返回获取到的目录项的数量。如果发生错误,则返回-1,并设置errno 以指示错误。

例子

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
struct dirent **namelist;
int n;

n = scandir(".", &namelist, NULL, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}

while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}

free(namelist);

exit(EXIT_SUCCESS);
}

运行结果

#./test
tdir
libc.c
libb.c
liba.c
gg.h
..
.

该结果是按照下标倒序显示的,也可以从下标 0 开始显示,这样就是按照字母排序的了。

使用 filter 参数进行过滤

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int myfilter(const struct dirent *entry)
{
return strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..");
}



int main(void)
{
struct dirent **namelist;
int n;

n = scandir(".", &namelist, myfilter, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}

while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}

free(namelist);

exit(EXIT_SUCCESS);
}

运行结果

#./test
tdir
libc.c
libb.c
liba.c
gg.h

获取以 lib 开头的文件

int myfilter(const struct dirent *ent)
{
if(ent->d_type != DT_REG)
return 0;

return (strncmp(ent->d_name, "lib", 3) == 0); 
}

运行结果如下:

#./test
libc.c
libb.c
liba.c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

短暂又灿烂的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值