概述
#include <string>
#include<io.h>
#include<queue>
class GetFileNumTool
{
public:
//获得所有文件个数
static int GetFileNum(const std::string& inPath)
{
int fileNum = 0;
std::vector<std::string> pathVec;
std::queue<std::string> q;
q.push(inPath);
while (!q.empty())
{
std::string item = q.front(); q.pop();
std::string path = item + "\*";
struct _finddata_t fileinfo;
auto handle = _findfirst(path.c_str(), &fileinfo);
if (handle == -1) continue;
while (!_findnext(handle, &fileinfo))
{
if (fileinfo.attrib & _A_SUBDIR)
{
if (strcmp(fileinfo.name , ".")==0 || strcmp(fileinfo.name , "..") == 0)continue;
q.push(item + "\" + fileinfo.name);
}
else
{
fileNum++;
pathVec.push_back(item + "\" + fileinfo.name);
}
}
_findclose(handle);
}
return fileNum;
}
//文件个数是否超过某个数值
static bool IsFolderFileNumTooBigger(const std::string& inPath,int maxNum)
{
int fileNum = 0;
//std::vector<std::string> pathVec;
std::queue<std::string> q;
q.push(inPath);
bool res = false;
while (!q.empty())
{
std::string item = q.front(); q.pop();
std::string path = item + "\*";
struct _finddata_t fileinfo;
auto handle = _findfirst(path.c_str(), &fileinfo);
if (handle == -1) continue;
while (!_findnext(handle, &fileinfo))
{
if (fileinfo.attrib & _A_SUBDIR)
{
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
q.push(item + "\" + fileinfo.name);
}
else
{
if (++fileNum > maxNum)
{
q = std::queue<std::string>();//清空并结束
res = true;
break;
}
//pathVec.push_back(item + "\" + fileinfo.name);
}
}
_findclose(handle);
}
return res;
}
//wstring 版本
static bool IsFileNumSmallThanMaxNum(const WCHAR* inPath,int maxNum,int& outNum,std::vector<std::wstring>& outVec)
{
bool res = true;
std::queue<std::wstring> q;
outNum = 0;
q.push(inPath);
//std::vector<std::wstring> vec;
outVec.clear();
while (!q.empty())
{
std::wstring item = q.front(); q.pop();
std::wstring path = item + L"\*";
WIN32_FIND_DATA winFindData;
HANDLE hTemp = FindFirstFile(path.c_str(), &winFindData);
if (INVALID_HANDLE_VALUE == hTemp) continue;
while (FindNextFile(hTemp, &winFindData))
{
std::wstring fileName = winFindData.cFileName;
if (L"." == fileName || L".." == fileName) continue;
//如果是目录
if (winFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
q.push(item + L"\" + winFindData.cFileName);
}
else
{
//if (++outNum > maxNum)
//{
// q = std::queue<std::wstring>();//清空并结束
// res = false;
// break;
//}
{
outVec.push_back(item + L"\" + winFindData.cFileName);
outNum++;
}
}
}
FindClose(hTemp);
}
return res;
}
};
最后
以上就是自然衬衫为你收集整理的C++ 获得文件夹中所有文件个数的全部内容,希望文章能够帮你解决C++ 获得文件夹中所有文件个数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复