您的位置:首页 > 运维架构 > Linux

linux C 递归找出一个路径下的所有文件

2017-02-05 00:58 417 查看
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

void dir_oper(char const*path);

int main(int argc, char const *argv[])
{
char const*path = argv[1];
struct stat s_buf;

/*获取文件信息,把信息放到s_buf中*/
stat(path,&s_buf);

/*判断输入的文件路径是否目录,若是目录,则往下执行,分析目录下的文件*/
if(S_ISDIR(s_buf.st_mode))
{
dir_oper(path);
}

/*若输入的文件路径是普通文件,则打印并退出程序*/
else if(S_ISREG(s_buf.st_mode))
{
printf("[%s] is a regular file\n",path);
return 0;
}

return 0;
}

void dir_oper(char const*path)
{
printf("[%s] it is a dir\n",path);
struct dirent *filename;
struct stat s_buf;
DIR *dp = opendir(path);

/*readdir()必须循环调用,要读完整个目录的文件,readdir才会返回NULL
若未读完,就让他循环*/
while(filename = readdir(dp))
{
/*判断一个文件是目录还是一个普通文件*/
char file_path[200];
bzero(file_path,200);
strcat(file_path,path);
strcat(file_path,"/");
strcat(file_path,filename->d_name);

/*在linux下每一个目录都有隐藏的. 和..目录,一定要把这两个排除掉。因为没有意义且会导致死循环*/
if(strcmp(filename->d_name,".")==0||strcmp(filename->d_name,"..")==0)
{
continue;
}

/*获取文件信息,把信息放到s_buf中*/
stat(file_path,&s_buf);

/*判断是否目录*/
if(S_ISDIR(s_buf.st_mode))
{
dir_oper(file_path);
printf("\n");
}

/*判断是否为普通文件*/
if(S_ISREG(s_buf.st_mode))
{
printf("[%s] is a regular file\n",file_path);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux c语言 递归