linux c递归遍历目录

本文能帮到你的话 就给个赞吧

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


void f(char *s){  //给一个字符串,遍历字符串

    //判断是不是文件
    struct stat buf;
    stat(s,&buf);

    if(S_ISREG(buf.st_mode)){
        printf("%s size : %d\n",s,buf.st_size);
        return ;
    }

    //不是文件便是目录

    DIR *dir;struct dirent *ptr;
    dir=opendir(s);

    chdir(s);
    //遍历目录
    while( (ptr=readdir(dir))!=NULL ){
        if( strcmp(ptr->d_name,".")==0||strcmp(ptr->d_name,"..")==0)
            continue;
        f(ptr->d_name);
    }

    //遍历结束
    closedir(dir);
    chdir("..");
    return ;
}

int main()
{

}


猜你喜欢

转载自blog.csdn.net/qq_42863961/article/details/89602809