目录
1. 只遍历当前一层文件夹下内容
#include <iostream>
#include <string>
#include <io.h>
int run_main()
{
// 图片所在文件夹,路径不要有中文
std::string imgRoot = "D:/";
_finddata64i32_t fileInfo;
intptr_t hFile = _findfirst((imgRoot + "\\*.*").c_str(), &fileInfo);
if (hFile == -1) {
std::cout << "not find image!\n";
return -1;
}
static int count = 0;
do
{
std::string imgPath = imgRoot + "/" + std::string(fileInfo.name);
std::cout << imgPath << std::endl;
count++;
} while (_findnext(hFile, &fileInfo) == 0);
return EXIT_SUCCESS;
}
2. 递归遍历当前文件夹下所有文件夹及子文件夹
这里需要用到 c++ 17
#include <iostream>
#include <vector>
#include <string>
#include <io.h>
#include <filesystem>
void curDir()
{
std::string imgRoot = "D:/";
using recursive_directory_iterator = std::filesystem::recursive_directory_iterator;
for (const auto& dirEntry : recursive_directory_iterator(imgRoot))
{
std::string filePath = dirEntry.path().string();
std::cout << filePath << std::endl;
}
}