#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>
void test_ItDir(const std::string& path)
{
DIR* dir = opendir(path.c_str());
if (dir == nullptr)
{
std::cerr << "Cannot open directory: " << path << std::endl;
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr)
{
std::string name = entry->d_name;
if (name == "." || name == "..")
{
continue;
}
std::string fullPath = path + "/" + name;
struct stat stat_buf;
if (stat(fullPath.c_str(), &stat_buf) != 0)
{
std::cerr << "Cannot stat file: " << fullPath << std::endl;
return;
}
// Determine whether it is a folder
if (S_ISDIR(stat_buf.st_mode))
{
std::cout << "Directory: " << fullPath << std::endl;
test_ItDir(fullPath);
}
else if (S_ISREG(stat_buf.st_mode))
{
std::cout << "File: " << fullPath << std::endl;
}
}
closedir(dir);
}
c++ 11遍历文件夹的文件
最新推荐文章于 2024-06-11 14:28:47 发布