遍历linux目录所需的毫秒
来源:互联网 发布:数值型数据库的特点 编辑:程序博客网 时间:2024/06/10 19:57
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <dirent.h>#include <time.h>#define PATHMAX 1024void getdir(char *path);void searchdir(char *path);void statlines(char *filepath);int total = 0;int main(void){ char dirpath[PATHMAX]; clock_t tick_start,tick_end; double t; tick_start=clock(); getdir(dirpath); searchdir(dirpath); tick_end=clock(); float dtime= (float)(tick_end-tick_start)/1000/1000; printf( "Total time used:%f second\n ",dtime); return 0;}void getdir(char *path){ int i = 0, ch; printf("Please input the full path of the catalog:"); while (i < PATHMAX && (ch = getchar()) != '\n') path[i++] = ch; path[i] = '\0';}void searchdir(char *path){ DIR *dp; struct dirent *dmsg; int i=0; char addpath[PATHMAX] = {'\0'}, *tmpstr; if ((dp = opendir(path)) == NULL) { perror("opendir"); exit(-1); } while ((dmsg = readdir(dp)) != NULL) { if (!strcmp(dmsg->d_name, ".") || !strcmp(dmsg->d_name, "..")) continue; strcpy(addpath, path); strcat(addpath, "/"); strcat(addpath, dmsg->d_name); //printf("....%s....",addpath); if (dmsg->d_type == 4) searchdir(addpath); else if (dmsg->d_type == 8) { if ((tmpstr = strrchr(dmsg->d_name, '.')) == NULL) continue; // if (!strcmp(tmpstr, ".c") || !strcmp(tmpstr, ".h")) // statlines(addpath); } } closedir(dp); }