scandir目录筛选出所有.c的文件,高手帮忙,谢谢!

zhouyunj 2012-10-27 12:10:18


//所要达到的目的: 我想使用scandir来实现筛选出 .c类型的文件
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

//自定义筛选回调函数
int myfilter(const struct dirent *filename)
{
char buf[20];
memset(buf,0,20);
int len=strlen(buf);
memcpy(buf,filename->d_name,len);
if(memcmp(filename->d_name,".c",10)==0) //这里比对方法好像不对?
{
return 0;
}
else
return 1;
}

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

n=scandir("./",&namelist,
myfilter,alphasort);
while(n--)
{
printf("%s\n",namelist[n]->d_name);
}

return 0;
}



1、我想实现对目录下筛选出所有的 .c 文件,好像我的筛选器写错了,不知道怎么写:(
2、如果我就想使用scandir实现子目录下也能筛选,怎么做?

是不是要用到S_ISDIR之类的判断?

希望此问题能得到全面解决,自学总是头疼啊!
谢谢!
...全文
506 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2012-10-27
  • 打赏
  • 举报
回复
[code=C/C++]
int myfilter(const struct dirent *filename)
{
size_t len;

len = strlen(filename->d_name);
if (len >= 2
&& filename->d_name[len - 2] == '.'
&& filename->d_name[len - 1] == 'c')
return 1;

return 0;
}
[/Code]
mymtom 2012-10-27
  • 打赏
  • 举报
回复
最简单的是用ftw函数
[code=C/C++]
#include <ftw.h>

#include <stdio.h>
#include <string.h>

int fn(const char *file, const struct stat *sb, int flag)
{
size_t len;

if (FTW_F == flag) {
len = strlen(file);
if (len >= 2
&& file[len - 2] == '.'
&& file[len - 1] == 'c')
printf("%s\n", file);
}
return 0;
}

int main(void)
{
ftw(".", fn, 5);
return 0;
}
[/Code]
mymtom 2012-10-27
  • 打赏
  • 举报
回复

#include <dirent.h>

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


void walk(const char *name)
{
int i, n;
size_t len;
struct dirent **list;
char *path;

n = scandir(name, &list, NULL, alphasort);
for (i = 0; i < n; i++) {
/* skip . && .. */
if (!strcmp(".", list[i]->d_name)
|| !strcmp("..", list[i]->d_name))
continue;
/* is a dir, recursive */
if (DT_DIR == list[i]->d_type) {
path = malloc(strlen(name) + 1 + strlen(list[i]->d_name) + 1);
sprintf(path, "%s/%s", name, list[i]->d_name);
walk(path);
free(path);
}

/* *.c */
len = strlen(list[i]->d_name);
if (len >= 2
&& list[i]->d_name[len - 2] == '.'
&& list[i]->d_name[len - 1] == 'c')
printf("%s/%s\n", name, list[i]->d_name);
}

free(list);
}

int main(void)
{
walk("/usr/src/sbin");

return 0;
}
zhouyunj 2012-10-27
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

int isDir(char *filename) //判断是否是目录
{
struct stat info;
stat(filename,&info);
if(S_ISDIR(info.st_mode))
return 1;
return 0;
}

int myfilter(const struct dirent *filename) //文件筛选器
{
size_t len;

len = strlen(filename->d_name);
if (len >= 2
&& filename->d_name[len - 2] == '.'
&& filename->d_name[len - 1] == 'c')
return 1;

return 0;
}

void show(char *filename,int(*myfilter)(const struct dirent *)) //显示文件名列表
{
int n;
struct dirent **namelist;
n=scandir(filename,&namelist,
myfilter,alphasort);

while(n--)
{
printf("%s\n",namelist[n]->d_name);
filename=strcat("./",namelist[n]->d_name); //使用递归子目录
if(isDir(filename))
{
show(filename,myfilter);
}
}
}

int main()
{
char *filename="./";
show(filename,myfilter);

return 0;
}


感谢大侠,感谢您给予的指点!
【问题】现在我想把它写成递归,列出所有子目录中包含"*.c"类型的文件!代码搞了好久还是搞不定,快疯了:(
注:不使用其它工具函数可以实现吗?谢谢!费心了,到时我追加分数!必须的一定的!
一句话概括: 由业余家谱爱好者基于Microsoft Office平台纯VBA开发,零成本、零门槛,帮你轻松制作媲美专业排印效果的家谱图表,还能一键生成网页版实现网络分享。 核心功能: 纯VBA代码开发,只要安装完整版MS Office即可运行(需支持Access数据库引擎)。 自动生成EXCEL格式的家谱图表,支持三种主流排版模式: 吊线图:直观呈现血缘脉络。 现代排版:适合打印装订的清晰版式。 欧式排版:传统家谱风格,保留古韵。 生成的排版结果完全可改可调,基于Excel单元格自由编辑调整。 新增重磅功能:支持生成静态HTML网页家谱,无需服务器、无需技术背景,直接双击打开就能分享给亲友查阅。 为什么选择丝连族谱? 零成本:基于Office开发,无需购买专业排版软件。 低门槛:纯VBA代码一键执行,不懂编程也能上手。 高自由度:生成的是标准Excel文件,可随意修改调整。 易分享:静态网页版可直接通过微信、U盘等方式传播,适合家族内部查阅。 使用前必读: 本工具基于Microsoft Office(含Access数据库引擎)开发,需要完整版Office支持。 32位Office 2010 及以上版本兼容;64位Office需使用2019及以上版本。 适用人群: 想修家谱但不想学专业排版软件的普通人 | 需要快速生成多份图表的分支编委 | 想将家谱数字化分享给亲友的家族传承者。

23,224

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧