linux c 获取文件数量

int get_folder_recording_cnt(char *root, int *cnt)
{
	DIR *dir;
	struct dirent * ptr;
	int total = 0;
	char path[128];
	
	dir = opendir(root); /* 打开bai目录*/
	if(NULL == dir) {
		printf("fail to open dir");
		
	}
	errno = 0;
	while(NULL != (ptr = readdir(dir))) {
		//顺序读取每一个目录项;
		//跳过“duzhi..”和“.”两个目录
		if(0 == strcmp(ptr->d_name,".") || 0 == strcmp(ptr->d_name,"..") ) {
			continue;
		}
		//printf("%s%s/n",root,ptr->d_name);
		//如果是目录,则递归dao调用 get_file_count函数
		if(ptr->d_type == DT_DIR) {
			sprintf(path,"%s%s/",root,ptr->d_name);
			//printf("%s/n",path);
			total += get_file_count(path);
		}
		if(ptr->d_type == DT_REG) {
			total++;
			printf("%s%s\n",root,ptr->d_name);
		}
	}
	if(0 != errno) {
		printf("fail to read dir"); //失败则输出提示信息
		
	}
	closedir(dir);
	
	*cnt = total;


	return 0;

}```

你可能感兴趣的:(linux,c)