首页 > 代码库 > 2017/04/23学习笔记

2017/04/23学习笔记

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <dirent.h>



int getFileNum(char* root){

    //open dir
    DIR* dir = NULL;
    dir = opendir(root);
    if(dir == NULL){
      perror("opendir");
      exit(1);
    }

    //遍历
    struct dirent* ptr = NULL;
    char path[1024] = {0};
    int total = 0;
    while((ptr = readdir(dir)) != NULL){
      if(strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..")==0){
        continue;
      }
      if(ptr->d_type==DT_DIR){
         sprintf(path,"%s/%s",root,ptr->d_name);
         total += getFileNum(path);
      }
      if(ptr->d_type==DT_REG){
         total++;
      }
    }
    closedir(dir);
    return total;
}


int main(){

  int result = getFileNum("/dafu");

  printf("result=%d",result);
  return 0;
}

 

2017/04/23学习笔记