C++ 递归读取目录下所有文件
windows版本
#include <iostream>
#include <io.h>
#include <fstream>
#include <string>
#include <sstream>
using namespace std; void getAllFiles(string path, vector<string>& files)
{
//文件句柄
long hFile = ;
//文件信息
struct _finddata_t fileinfo; //很少用的文件信息读取结构
string p; //string类很有意思的一个赋值函数:assign(),有很多重载版本
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR)) //比较文件类型是否是文件夹
{
if (strcmp(fileinfo.name, ".") != && strcmp(fileinfo.name, "..") != )
{
files.push_back(p.assign(path).append("/").append(fileinfo.name));
getAllFiles(p.assign(path).append("/").append(fileinfo.name), files);
}
}
else
{
files.push_back(p.assign(path).append("/").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == ); //寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
} int main(){
char * inPath = "./srcImg";
vector<string> files;
//测试
char * distAll = "AllFiles.txt";
getAllFiles(inPath, files);
ofstream ofn(distAll);
int size = files.size();
ofn << size << endl;
for (int i = ; i<size; i++)
{
ofn << files[i] << endl;
}
ofn.close(); return ;
}
linux版本
#include <iostream>
#include <string>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
extern "C"{
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
using namespace std; void getAllFiles(string path, vector<string>& files)
{
DIR *dir;
struct dirent *ptr;
if((dir=opendir(path.c_str()))==NULL){
perror("Open dri error...");
exit();
}
while((ptr=readdir(dir))!=NULL){
if(strcmp(ptr->d_name,".")==||strcmp(ptr->d_name,"..")==)
continue;
else if(ptr->d_type==)//file
files.push_back(path+"/"+ptr->d_name);
else if(ptr->d_type==)//link file
continue;
else if(ptr->d_type==){
//files.push_back(ptr->d_name);//dir
getAllFiles(path+"/"+ptr->d_name,files);
}
}
closedir(dir);
}
int main(int argc,char **argv){
if(argc<){
cout<<"USAGE:./a.out path"<<endl;
exit(-);
}
char * filePath = argv[];
vector<string> files;
char * distAll = "allFiles.txt";
getAllFiles(filePath, files);
ofstream ofn(distAll);
int size = files.size();
//ofn << size << endl;
for (int i = ; i<size; i++)
{
ofn << files[i] << endl;
}
ofn.close();
return ;
}
C++ 递归读取目录下所有文件的更多相关文章
- (实用篇)PHP不用递归遍历目录下所有文件的代码
<?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...
- Java递归列出目录下全部文件
Java递归列出目录下全部文件 /** * 列出指定目录的全部内容 * */ import java.io.*; class hello{ public static void main(String ...
- php读取目录下的文件
工作需要写了一个读取指定目录下的文件,并显示列表,点击之后读取文件中的内容 高手拍砖,目录可以自由指定,我这里直接写的是获取当前文件目录下面的所有文件 <?php /** * 读取指定目录下面的 ...
- Python递归遍历目录下所有文件
#自定义函数: import ospath="D:\\Temp_del\\a"def gci (path): """this is a stateme ...
- linux递归查找目录下所有文件夹以及文件
相对于ls或者ll,可能find在这个时候更加给力 先看我的目录结构 tree命令是查看目录的结构,而且最后会列出所有的directory的数目以及文件夹的数目...好像我们接下来要做的就没有必要了, ...
- python递归获取目录下指定文件
获取一个目录下所有指定格式的文件是实际生产中常见需求. import os #递归获取一个目录下所有的指定格式的文件 def get_jsonfile(path,file_list): dir_lis ...
- File类 递归 获取目录下所有文件文件夹
package com.xiwi; import java.io.*; import java.util.*; class file{ public static void main(String a ...
- day1 diff命令递归比较目录下的文件
一.diff实战 (1)递归比较文件夹下所有的文件及目录的不同 diff --brief -Nr dir1/ dir2/ Reference ...
- php递归获取目录下所有文件
<?php function getFileList($dir){ $dir=iconv("utf-8","gb2312",$dir); if ($hea ...
随机推荐
- MySQL存储引擎与索引
引言: MySQL存储引擎主要分为 InnoDB 存储引擎与 MyISAM 存储引擎.都采用B+数的存储结构. 应用场景: InnoDB适合:(1)可靠性要求比较高,要求事务:(2)大量 insert ...
- Mysql jar包
密码cngb https://pan.baidu.com/share/init?surl=bSGA6T-LTwjx-qaNAiipCA
- iOS之UITableView中的cell因为重用机制导致新的cell的数据出现重复或者错乱
UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚 ...
- DBCacheServer升级
前段时间完成了该服务的设计的功能,花了很多时间和经历,最终完成了一个版本,已经测试了:现在后期再次在以前的基础上,完成了一些扩展. 1.扩展了内存存储 最初版本只是采用了gauva cache进行存储 ...
- pushlet(QQ提示框)
Pushlet 实现服务端往客服端推送消息 系统页面弹出消息框,类似QQ提示框 1. java代码 package com.test.jbpm.common; import java.io.Seria ...
- 随便说说Promise
为啥要说 promise ? 因为这是前端必须要掌握的一个知识,吹逼必备 首先说说 Promise 是什么? Promise 是JavaScript的第一个异步标准模型,一个包含传递信息与状态的对象, ...
- vue.esm.js:578 [Vue warn]: Missing required prop
问题: 解决: required: true,属性是,这个必须填写
- ABAP术语-Connection Type
Connection Type 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/17/1042479.html A connection ty ...
- 如何用 npm 同时执行两条监听命令
在日常项目中启动项目 需要启动项目可能需要不止一条命令 这就很麻烦 要开启两个bash 很麻烦 终于找到了比较好的解决方案 例如我的: npm run dev //启动项目项目 npm run jso ...
- grafana使用Prometheus数据源监控mongo数据库
数据库改用mongo后,监控需求就需要整合进grafana里,由于一直在坚持docker化部署,那么此次也不例外. 1. 安装Prometheus: What is Prometheus? Prome ...