遍历文件夹下的所有文件:代码实现
#include <iostream>
#include <dirent.h>
#include <vector>
#include <cstring>
void GetFileNames(std::string path, std::vector<std::string> &filenames)
{
DIR *pDir;
struct dirent* ptr;
if(!(pDir = opendir(path.c_str()))){
std::cout<<"Folder doesn't Exist!"<<std::endl;
return;
}
while((ptr = readdir(pDir))!=0) {
if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){
filenames.push_back(path + "/" + ptr->d_name);
}
}
closedir(pDir);
}
int main() {
std::vector<std::string> file_name;
std::string path = "/home/mi/桌面/res_imgs";
GetFileNames(path, file_name);
for(int i = 0; i <file_name.size(); i++)
{
std::cout<<file_name[i]<<std::endl;
}
return 0;
}
输出结果: 参考:https://www.cnblogs.com/gdut-gordon/p/9678175.html
|