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 ...
随机推荐
- LeetCode12.整数转罗马数字 JavaScript
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- nginx 方向代理
#nginx 监听原理 先监听端口 --> 再配置域名 -->匹配到就访问local 否则 没有匹配到域名就默认访问第一个监听端口的local地址 # vi nginx.conf user ...
- linux mariadb
https://www.linuxidc.com/Linux/2016-03/128880.htm -- sql 导入 接着输入你所导入到Centos下的数据库文Student.sql文件的位置例如: ...
- 第一次接触mysql
一:数据库的好处 1. 可以持久化保存数据在本地 2.结构化查询 二:数据库常见的概念 1.DB: 是datebase数据库的缩写,储存数据得到容器 2.DBMS:数据库管理系统,用于管理数据库,和创 ...
- mysql快速导入导出数据
--导入 select * from inhos_genoperation(表名) where UPLOAD_ORG_CODE='***' into outfile '/tmp/inhos_genop ...
- 封装一个方法获取url上面的参数
一.取参 ] : ); ]; ; ]., -); ]) === ]; , , b: 'fdfdfd', c: '9999' })); //a=123546&b=fdfdfd&c=9 ...
- LeetCode 中级 - 从前序与中序遍历序列构造二叉树(105)
一个前序遍历序列和一个中序遍历序列可以确定一颗唯一的二叉树. 根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root), 然后在中 ...
- Debug实验学习汇编
R命令查看.改变CPU寄存器的内容: D命令查看内存中的内容: E命令改写内存中的内容: U命令将内存中的机器指令翻译成汇编指令: T命令执行一条机器指令: A命令以汇编指令的格式在内存中写入一条机器 ...
- ab工具测试 swoole 和 ngixn+php-fpm 的并发对比
测试样例: 执行的一条sql记录的1w次插入分两组: 一组用nginx+pfm 来执行, 一组用swoole 来执行 公平性保证前提: @1.为了保证公平性, 在nginx里把 access_log, ...
- poj 3177 Redundant Paths 求最少添加几条边成为双联通图: tarjan O(E)
/** problem: http://poj.org/problem?id=3177 tarjan blog: https://blog.csdn.net/reverie_mjp/article/d ...