readDir读取目录下的图片,保存到vector中 支持jpg格式,其他需要修改代码
#include "dirent.h"
int readDir(char *basePath, vector<string> &filelist)
{
DIR *dir;
struct dirent *ptr;
char base[1000];
if ((dir = opendir(basePath)) == NULL)
{
printf("readDir:Open dir error...\n");
exit(1);
}
while ((ptr = readdir(dir)) != NULL)
{
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
{
//printf("strcmp(ptr->d_name,\n");
continue;
}
else if (ptr->d_type == 8) ///file
{
char *pszFileType = NULL;
pszFileType = &(ptr->d_name[strlen(ptr->d_name) - 3]);
if (!strcasecmp(pszFileType, "jpg"))
{
string str_basePath(basePath);
string str_d_name(ptr->d_name);
string str_szFullPath = str_basePath + "/" + str_d_name;
filelist.push_back(str_szFullPath);
}
else
{
//printf("is not jpg!\n");
}
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
}
/*else if (ptr->d_type == 10)
{
} *////link file
//printf("d_name:%s/%s\n",basePath,ptr->d_name);
else if (ptr->d_type == 4) ///dir
{
memset(base, '\0', sizeof(base));
strcpy(base, basePath);
strcat(base, "/");
strcat(base, ptr->d_name);
readDir(base, filelist);
}
else
{
//printf("load img error!\n");
}
}
closedir(dir);
sort(filelist.begin(), filelist.end());
return 1;
}