linux下用C语言实现指定文件的搜索

benq520 2006-05-19 11:20:46
请各位达人帮忙:
请在linux下用C语言实现指定文件的搜索,具体要求如下:搜索一个指定文件夹中的指定后缀名的文件(例如:*.mp3,*.mwv),输出查找到的文件个数,当文件不存在是提示错误信息!
...全文
547 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenuy 2006-05-22
  • 打赏
  • 举报
回复
也可以直接调用系统函数
system("find dirname -name filename")
还可以加各种参数,其实某些时候可以直接调用
效率不会底多少
benq520 2006-05-22
  • 打赏
  • 举报
回复
此区高手如云,小弟受教很多 !希望高人继续指教!
lbaby 2006-05-20
  • 打赏
  • 举报
回复
fnmatch shell通配符匹配
http://www.opengroup.org/onlinepubs/000095399/functions/fnmatch.html


regcomp 及regexec正则表达式编译和匹配
http://www.opengroup.org/onlinepubs/000095399/functions/regcomp.html


EXAMPLES
#include <regex.h>

/*
* Match string against the extended regular expression in
* pattern, treating errors as no match.
*
* Return 1 for match, 0 for no match.
*/

int
match(const char *string, char *pattern)
{
int status;
regex_t re;

if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
return(0); /* Report error. */
}
status = regexec(&re, string, (size_t) 0, NULL, 0);
regfree(&re);
if (status != 0) {
return(0); /* Report error. */
}
return(1);
}

铖邑 2006-05-19
  • 打赏
  • 举报
回复
好象只有readdir,不能搜索

/* opendir.c - test opendir(), readdir(), closedir() */

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

void scandir(char *dirname)
{
DIR *dir;
struct dirent *ent;

printf("First pass on '%s':\n",dirname);
if ((dir = opendir(dirname)) == NULL)
{
perror("Unable to open directory");
exit(1);
}
while ((ent = readdir(dir)) != NULL)
printf("%s\n",ent->d_name);

printf("Second pass on '%s':\n",dirname);

rewinddir(dir);
while ((ent = readdir(dir)) != NULL)
printf("%s\n",ent->d_name);
if (closedir(dir) != 0)
perror("Unable to close directory");
}

void main(int argc,char *argv[])
{
if (argc != 2)
{
printf("usage: opendir dirname\n");
exit(1);
}
scandir(argv[1]);
exit(0);
}
phus 2006-05-19
  • 打赏
  • 举报
回复
更正一下
#include <stringsx.h>
应为
#include <strings.h>
phus 2006-05-19
  • 打赏
  • 举报
回复
嗯,这个demo比较简单, 具体如何做, 楼主还需man nftw
phus 2006-05-19
  • 打赏
  • 举报
回复
用ntfw可以作出来


#define _XOPEN_SOURCE 1
#define _XOPEN_SOURCE_EXTENDED 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for strrchr(2)
#include <stringsx.h> //for strcasecmp(2)
#include <unistd.h>
#include <sys/types.h>
#include <ftw.h>

size_t file_cnt = 0;

int process(const char *file, const struct stat *sb, int flag, struct FTW *s)
{
char *ext;

ext = strrchr(file, '.');
if(ext == NULL)
return 0;
if(strcasecmp(ext, ".mp3") == 0)
file_cnt++;
return 0;
}

int main(int argc, char *argv[])
{
int nfds;

nfds = getdtablesize()/2 - 10;
nftw(argv[1], process, nfds, FTW_CHDIR);
printf("%d\n", file_cnt);
return 0;
}
9653013 2006-05-19
  • 打赏
  • 举报
回复
ri
benq520 2006-05-19
  • 打赏
  • 举报
回复
3楼的达人说的很在理。能给出具体实现的代码吗?
wenuy 2006-05-19
  • 打赏
  • 举报
回复
2楼要写递归
你那样子就查一级目录
还需要继续往下读的
还有要对文件目录比较
楼主参考一下就可以了
benq520 2006-05-19
  • 打赏
  • 举报
回复
谢谢上面的达人!
我再把要求说的具体点:在指定的目录下一层一层的打开下面的子目录,然后在找出这个目录中(包括所有子目录)中以mp3为后缀名的文件,并统计找到的文件个数。
达人请继续赐教!

23,121

社区成员

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

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