Fork me on GitHub

版权声明 本站原创文章 由 萌叔 发表
转载请注明 萌叔 | https://vearne.cc
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
using namespace std;
int is_regular_file(char * a){
    if(*a=='.'){
        return 0;
    }else{
        return 1;
    }
}
char* append(const char * a,const char *b){
    char* buffer = new char[256];
    strcpy(buffer,a);
    strcat(buffer,"/");
    strcat(buffer,b);
    return buffer;
}
void tree(char* argv,int level){
    DIR* dirptr;
    struct stat buf;
    stat(argv,&buf);
    if(!S_ISDIR(buf.st_mode)){
        return; 
    }
    //open file
    if((dirptr = opendir(argv))!=NULL){
        struct dirent* entry;
        while(entry=readdir(dirptr)){
            if(!is_regular_file(entry->d_name)){
                continue;
            }       
            for(int i=0;i<level;i++){
                printf("\t");
            }
            printf("|");
            printf("--");
            printf("%s\n",entry->d_name);
            tree(append(argv,entry->d_name),level+1);
        }
        //close file
        closedir(dirptr);
    }
}
int main(int argc,char* argv[]){

    tree(argv[1],0);
}

后记

这是我2012年的文章,原来发表在ITeye,现在一并迁移过来


请我喝瓶饮料

微信支付码

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据