c++ 遍历目录下的文件
发布日期:2022-03-11 10:18:57 浏览次数:31 分类:技术文章

本文共 1443 字,大约阅读时间需要 4 分钟。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;// 递归列出所有目录及文件void recursion_scan_dir_file(const char *dir, int depth=1){ DIR *p_dir = NULL; struct dirent *p_entry = NULL; struct stat statbuf; if((p_dir = opendir(dir)) == NULL) { printf("can't open dir.\n"); return; } chdir (dir); while(NULL != (p_entry = readdir(p_dir))) { // 获取下一级目录信息 lstat(p_entry->d_name, &statbuf); // 获取下一级成员属性 if(S_IFDIR & statbuf.st_mode) { // 判断下一级成员是否是目录 if (strcmp(".", p_entry->d_name) == 0 || strcmp("..", p_entry->d_name) == 0) continue; printf("%*s%s/\n", depth, "", p_entry->d_name); recursion_scan_dir_file(p_entry->d_name, depth+4); // 扫描下一级目录的内容 } else { printf("%*s%s\n", depth, "", p_entry->d_name); // 输出属性不是目录的成员 } } chdir(".."); // 回到上级目录 closedir(p_dir);}//列出一个目录下所有文件void scan_one_dir( const char * dir_name, std::vector
&files_vector){ if( NULL == dir_name ) { cout<<" dir_name is null ! "<
d_name , "." ) == 0 || strcmp( filename->d_name , "..") == 0) continue; char wholePath[128] = {0}; sprintf(wholePath, "%s", filename->d_name); files_vector.push_back(wholePath); }} main(int argc, char* argv[]){ string file_dir = "/home/"; std::vector
files_vector; scan_one_dir(file_dir.c_str(), files_vector); recursion_scan_dir_file(file_dir.c_str(), 1); for (auto iters : files_vector) { cout << iters << std::endl; } return 0;}

 

转载地址:https://blog.csdn.net/weicao1990/article/details/110479531 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:linux下通过shell命令将某目录下图片路径导入到指定的文件中
下一篇:opencv3中的glob函数读取文件夹中数据

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月02日 05时36分09秒