linux c/c++判断文件夹文件个数(或者说是否为空)
直接上代码:
#include <unistd.h> #include <sys/types.h> #include <dirent.h> int API::TraverseDir_Num(const char* strVideoDir)//返回该文件夹的文件个数 { //遍历目录 static int num = 0; DIR* dp; struct dirent *entry; struct stat statbuf; dp = opendir(strVideoDir); if(!dp) { LLERROR("无法打开目录:%s", strVideoDir); return -1; } chdir(strVideoDir); while((entry = readdir(dp)) != nullptr) { lstat(entry->d_name, &statbuf); if(S_ISDIR(statbuf.st_mode)) { if(!strcmp(".",entry->d_name) || !strcmp("..",entry->d_name)) { continue; } char strNewDir[256]; sprintf(strNewDir, "%s/%s", strVideoDir, entry->d_name); TraverseDir_Num(strNewDir); } else { num += 1; } }; chdir(".."); closedir(dp); return num; }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: Java对象池
- 下一篇: sql 中模糊查询转义 escape