Created
March 8, 2019 01:13
-
-
Save hewumars/4dbbd4470cb41e980396264d08293ee7 to your computer and use it in GitHub Desktop.
readDir读取目录下的图片,保存到vector<string>中
支持jpg格式,其他需要修改代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment