linux下指定目录下按文件类型搜索。

AIGPTchina 2012-09-15 06:47:13
linux下指定目录下按文件类型搜索。

例如:已知某目录下有三个文件
a
a.c
a.c.bak
b.c

如何用函数返回.c的文件。
a.c
b.c
...全文
434 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hnzmdzcm 2012-09-16
  • 打赏
  • 举报
回复
find / -type
Topseten 2012-09-16
  • 打赏
  • 举报
回复
二楼的正解
通用的写法有:
ls | grep *.你的文件类型
或者使用find命令
find 你查找的路径 -name *.你的文件类型

命令里面的*代表所有匹配的字符,即 文件为 abc.yourfiletype ddd.yourfiletype
你使用ls | grep *.yourfiletype
*则代表 .yourfiletype前面任意匹配的字符
不知道这么解释你能不能明白
AIGPTchina 2012-09-16
  • 打赏
  • 举报
回复
std::vector<std::string> files;
std::vector<std::string> matched_files;
能放到C里用吗?
mujiok2003 2012-09-16
  • 打赏
  • 举报
回复

std::vector<std::string> files;
std::vector<std::string> matched_files;
for(size_t i = 0, size = files.size(); i < size; ++i)
{
std::string const& file = files[i];
size_t const len = file.length();
if(len > 2 && file[len-1] == 'c' && file[len-2] = '.')
{
matched_files.push_back(file);
}
}


或用正则匹配".c$"
AIGPTchina 2012-09-16
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <string.h>
#define FILE_NUM 10 // 文件个数
#define FILE_SIZEOF 40 //文件名大小
//判断是否为目录
int fun(char strsz[FILE_NUM][FILE_SIZEOF],char destsz[FILE_NUM][FILE_SIZEOF],int *dest_num);
int IS_DIR(const char* path)
{
struct stat st;
lstat(path, &st);
return S_ISDIR(st.st_mode);
}

//遍历文件夹de递归函数
void List_Files_Core(const char *path, int recursive)
{
DIR *pdir;
struct dirent *pdirent;
char p[10];
char temp[256];
char deststr[FILE_NUM][FILE_SIZEOF];
char strsz[FILE_NUM][FILE_SIZEOF];
int i = 0 ;
int j;
int k;
int len;
int num;
pdir = opendir(path);
memset(strsz,0,sizeof(strsz));
if(pdir)
{
while(pdirent = readdir(pdir))
{
//跳过"."和".."
if(strcmp(pdirent->d_name, ".") == 0
|| strcmp(pdirent->d_name, "..") == 0)
{

continue;
}
sprintf(temp, "%s/%s", path, pdirent->d_name);
//i++;
//printf("\ni=%d\n",i);
//printf("\nstrlen(pdirent->d_name)=%d",strlen(pdirent->d_name));
memcpy(&strsz[i],pdirent->d_name,strlen(pdirent->d_name));

i++;
//当temp为目录并且recursive为1的时候递归处理子目录
if(IS_DIR(temp) && recursive)
{
List_Files_Core(temp, recursive);
}
}
}
else
{
printf("opendir error:%s\n", path);
}
closedir(pdir);


fun(strsz,deststr,&num);
if(num==1)
{
printf("\nstrlen(deststr[0])=%d\n",strlen(deststr[0]));
printf("%s",deststr[0]);
//for(k=0;k<FILE_SIZEOF;k++)
//printf("deststr[%d][%d]%d",deststr[1][k]));
printf("\n++++++++++++++++:\n");
}

}
//遍历文件夹的驱动函数


int fun(char strsz[FILE_NUM][FILE_SIZEOF],char destsz[FILE_NUM][FILE_SIZEOF],int *dest_num)
{
const char test[]=".bin";
char temp[FILE_SIZEOF];
//char destsz[10][20]; // 存放按指定的数据类型找到的文件名。

int j,len,num=0,k,m;
memset(temp,0,sizeof(temp));

for(j =0 ;j<FILE_NUM;j++)
{
len = strlen(strsz[j]);
if(len<2)
continue;
if(len>4)
{
memcpy(temp,&strsz[j][len-4],4);
//printf("temp[0]=%02x\t",temp[0]);
//printf("temp[1]=%02x\t",temp[1]);

//printf("\nj=%d\n",j);
if(!memcmp(test,temp,4))
{
memcpy(destsz[num],strsz[j],FILE_SIZEOF);
printf("\nok,num=%d\n",num);
num++;
}
else
{
//printf("\nNok\n");
}
}
}
if(num==0) //若没找到有.ini结束的就直接退出
{
return 0;
}
*dest_num = num;
#if 0
printf("\ndest_num=%d\n",*dest_num);
for(k=0;k<num;k++) //若找到了,就把他们都打印出来。
{
for(m=0;m<FILE_SIZEOF;m++)
{
printf("destsz[%d][%d]=%02x\t",k,m,destsz[k][m]);
}
printf("\n------------------------------------------\n");
}
#endif
return 1;
}
void List_Files(const char *path, int recursive)
{
int len;
char temp[256];
char *p= NULL;
//去掉末尾的'/'
len = strlen(path);
strcpy(temp, path);
if(temp[len - 1] == '/') temp[len -1] = '\0';

if(IS_DIR(temp))
{
//处理目录
List_Files_Core(temp, recursive);
}
else //输出文件
{
// p = strchr(path,'P');
// printf("%s\n", p);
}
}

int main(int argc, char** argv)
{
// if(argc != 2)
// {
// printf("Usage: ./program absolutePath\n");
// exit(0);
// }
//strchr();
char str[]="/mnt/hgfs/";
List_Files(str, 1);
return 0;
}
用这个代码可以解决。谢谢大家指教。恳请大侠们加我QQ:278359100,若有问题可以及时问询。
ForestDB 2012-09-15
  • 打赏
  • 举报
回复
man dirent
man opendir
man readdir
遍历目录下的dirent,看其中d_name栏位是否以.c结尾。
羽飞 2012-09-15
  • 打赏
  • 举报
回复
ls *.c
qq120848369 2012-09-15
  • 打赏
  • 举报
回复
ls *.c
find . -name *.c
AIGPTchina 2012-09-15
  • 打赏
  • 举报
回复
例如:已知某目录下有个文件
a
a.c
a.c.bak
b.c

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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