查找文件夹下所有文件方法c++
代码如下:
void EnumFiles(const char *directory, int *count, char **result, int flag)//flag:2 bmp 1 bin
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char pattern[MAX_PATH];
int i = 0;
char dir[MAX_PATH];
// 开始查找
strcpy(pattern, directory);
strcat(pattern, "\\*.*");
hFind = FindFirstFile(pattern, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
*count = 0;
}
else
{
do
{
if (flag == 0 && i >= MAX_IMG_NUM)
continue;
if ((flag == 1 || flag == 2) && i >= MAX_IMG_NUM)
continue;
if (strcmp(FindFileData.cFileName, ".") == 0 || strcmp(FindFileData.cFileName, "..") == 0)
continue;
memset(dir, 0, MAX_PATH);
strcpy(dir, directory);
strcat(dir, "\\");
strcat(dir, FindFileData.cFileName);
if (flag == 1)
{
int l = strlen(dir);
if (dir[l - 1] == 'n' && dir[l - 2] == 'i' && dir[l - 3] == 'b')
{
strcpy(result[i++], dir);
}
else
continue;
}
else if (flag == 2)
{
int l = strlen(dir);
if (dir[l - 1] == 'p' && dir[l - 2] == 'm' && dir[l - 3] == 'b')
{
strcpy(result[i++], dir);
}
else
continue;
}
else
strcpy(result[i++], dir);
} while (FindNextFile(hFind, &FindFileData) != 0);
}
// 查找结束
FindClose(hFind);
// 复制到结果中
*count = i;
}