标签:sprint turn add close pat null sprintf file blog
#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; }
标签:sprint turn add close pat null sprintf file blog
原文地址:http://www.cnblogs.com/dafume/p/6754097.html