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之类的判断?

希望此问题能得到全面解决,自学总是头疼啊!
谢谢!
...全文
469 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"类型的文件!代码搞了好久还是搞不定,快疯了:(
注:不使用其它工具函数可以实现吗?谢谢!费心了,到时我追加分数!必须的一定的!

23,216

社区成员

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

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