牛骨文教育服务平台(让学习变的简单)
博文笔记

linux c/c++判断文件夹文件个数(或者说是否为空)

创建时间:2017-08-01 投稿人: 浏览次数:1636

直接上代码:


#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;
}


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。