您的位置:首页 > 编程语言 > C语言/C++

c语言遍历某文件夹下的所有文件 包括子文件夹

2016-08-28 13:27 465 查看
void printdir(char *dir, int depth)

{

    DIR *dp;

    struct dirent *entry;

    struct stat statbuf;

    if( (dp = opendir(dir)) == NULL ){

        fprintf(stderr,"cannot open directory: %s\n", dir);

        return;

    }
fprintf(stderr,"cannot open directory: %s\n", dir);

    chdir(dir);

    while((entry = readdir(dp)) != NULL) {

        lstat(entry->d_name,&statbuf);

        if( S_ISDIR(statbuf.st_mode) ){

            if( strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 ){

                continue;

            }

            LOGE("%*s%s/\n",depth,"",entry->d_name);
if( strcmp("sys",entry->d_name) == 0 || strcmp("proc",entry->d_name) == 0 ){
//ÎļþÌ«ËûÂèµÄ¶àÁË

                continue;
}

            printdir(entry->d_name,depth+4);

        }else{

            LOGE("%*s%s\n",depth,"",entry->d_name);
}

    }

    chdir("..");

    closedir(dp);

}

static int recovery_test( void ) 

{

    saved_log_file* head = NULL;   

    DIR* d;

    struct dirent* de;

    d = opendir("/");

    if( d ){

        char path[PATH_MAX];

        strcpy(path, "/");

        //strcat(path, "/");

        int path_len = strlen(path);

        while( (de = readdir(d)) != NULL ){
LOGE("de->d_name: %s\n", de->d_name);

            if( strncmp(de->d_name, "last", 4) == 0 ){

                

            }

        }

        closedir(d);

    } else {

        if (errno != ENOENT) {

            LOGE("opendir failed: %s\n", strerror(errno));

        }

    }

    ui->Print("in test\n" );

printdir("/",0);

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: