#include <iostream>
#include <fstream>
#include <dirent.h>
#include <vector>
#include <algorithm>
std::vector<std::string> GetFileNamesInDir(const std::string& folderPath)
{
DIR* dir = opendir(folderPath.c_str());
std::vector<std::string> fileNames;
if (dir != NULL)
{
struct dirent* entry;
while ((entry = readdir(dir)) != NULL)
{
std::string fileName = entry->d_name;
if (fileName != "." && fileName != "..")
{
fileNames.push_back(fileName);
}
}
closedir(dir);
}
std::sort(fileNames.begin(), fileNames.end()); // 排序
return fileNames;
}
int main()
{
std::string img_dir = "/data/";
std::vector<std::string> fileNames = GetFileNamesInDir(img_dir);
for(int i=0; i < fileNames.size(); i++)
{
std::string file_path = img_dir + fileNames[i];
std::cout << "file_path: " << file_path << std::endl;
}
return 0;
}
C++ 通过文件夹按顺序读取文件列表
最新推荐文章于 2024-03-05 08:45:24 发布