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 ...
随机推荐
- javascript 六种基本数据类型转换
javascript 六种基本数据类型转换 1.显式转换 通过手动进行类型转换,Javascript提供了以下转型函数: 转换为数值类型:Number(mix).parseInt(string,rad ...
- 更换eclipse字体
eclipse自带的字体非常不好,看的我难受,可能是使用myeclipse习惯了,怎么调节都不好使 最后决定下载一个字体包吧! 字体下载地址如下:http://files.cnblogs.com/ic ...
- Percona-Tookit工具包之pt-index-usage
Preface There're many ways relevent with performance tuning.For example,using indexes proper ...
- 如何安全地跨窗体调用Timer控件 从一个窗体调用控制另外一个窗体的控件
具体的情况是Form1中有一个Timer2时钟,Timer2时钟事件弹出Warning窗体,点击Warning窗体上面的按钮,重新激活一下Form1中的Timer2.从而实现了从一个窗体调用另外一个窗 ...
- 剑指offer—二维数组中的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- webpack 之 webpack-dev-server自动刷新
watch 首先介绍watch选项,参考这里.可实现相关源文件改变后自动更新bundle.js文件的功能.在配置文件中添加 watch:true 或执行 webpack -w,即可开启watch功能: ...
- hdcms v5.7.0学习笔记
hdcms v5.7.0学习笔记 https://note.youdao.com/ynoteshare1/index.html?id=c404d63ac910eb15a440452f73d6a6db& ...
- 日志框架Log4j
log4j是一个用Java编写的可靠,快速和灵活的日志框架(API),它在Apache软件许可下发布.Log4j已经被移植到了C,C++,C#,Perl,Python和Ruby等语言中. Log4j是 ...
- python应用:异常处理
Python的错误异常在大部分IDE编辑器中则可以直接显示出来,便于开发人员的调试及修改工作,对初学者也比较友好. Python中包含错误和异常两种情况,错误主要是常见的语法错误SyntaxError ...
- django的查询集
查询集表示从数据库中获取的对象集合,在管理器上调用某些过滤器方法会返回查询集,查询集可以含有零个.一个或多个过滤器.过滤器基于所给的参数限制查询的结果,从Sql的角度,查询集和select语句等价,过 ...