Skip to content

Instantly share code, notes, and snippets.

@SolemnJoker
Last active November 7, 2020 06:51
Show Gist options
  • Save SolemnJoker/4962b058eb451287e24d9bb0c4b54ba0 to your computer and use it in GitHub Desktop.
Save SolemnJoker/4962b058eb451287e24d9bb0c4b54ba0 to your computer and use it in GitHub Desktop.
[get files list ] 获取文件列表 #c++ #file
inline
std::vector<std::string> getFilesList(std::string dirpath){
DIR *dir = opendir(dirpath.c_str());
if (dir == NULL)
{
std::cout << "opendir error" << std::endl;
}
std::vector<std::string> all_path;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_DIR){//It's dir
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
std::string dirNew = dirpath + "/" + entry->d_name;
if(strcmp(dirNew.substr(dirNew.length() - 4,dirNew.length()).c_str(),".msg") != 0){
continue;
}
std::vector<std::string> tempPath = getFilesList(dirNew);
all_path.insert(all_path.end(), tempPath.begin(), tempPath.end());
}else {
std::string name = entry->d_name;
std::string imgdir = dirpath +"/"+ name;
//sprintf("%s",imgdir.c_str());
all_path.push_back(imgdir);
}
}
closedir(dir);
//system("pause");
return all_path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment