C++读取文件夹中所有的文件或者是特定后缀的文件
2014-09-20 15:48
961 查看
由于经常有读取一个文件夹中的很多随机编号的文件,很多时候需要读取某些特定格式的所有文件。
下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件
参考:http://www.2cto.com/kf/201407/316515.html
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//获取所有的文件名
void GetAllFiles( string path, vector<string>& files)
{
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
{
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);
_findclose(hFile);
}
}
//获取特定格式的文件名
void GetAllFormatFiles( string path, vector<string>& files,string format)
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(),&fileinfo)) != -1)
{
do
{
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
{
//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
GetAllFormatFiles( p.assign(path).append("\\").append(fileinfo.name), files,format);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
// 该函数有两个参数,第一个为路径字符串(string类型,最好为绝对路径);
// 第二个参数为文件夹与文件名称存储变量(vector类型,引用传递)。
// 在主函数中调用格式(并将结果保存在文件"AllFiles.txt"中,第一行为总数):
int main()
{
string filePath = "testimages\\water";
vector<string> files;
char * distAll = "AllFiles.txt";
//读取所有的文件,包括子文件的文件
//GetAllFiles(filePath, files);
//读取所有格式为jpg的文件
string format = ".jpg";
GetAllFormatFiles(filePath, files,format);
ofstream ofn(distAll);
int size = files.size();
ofn<<size<<endl;
for (int i = 0;i<size;i++)
{
ofn<<files[i]<<endl;
cout<< files[i] << endl;
}
ofn.close();
return 0;
}
下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件
参考:http://www.2cto.com/kf/201407/316515.html
#include <io.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//获取所有的文件名
void GetAllFiles( string path, vector<string>& files)
{
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
{
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);
_findclose(hFile);
}
}
//获取特定格式的文件名
void GetAllFormatFiles( string path, vector<string>& files,string format)
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(),&fileinfo)) != -1)
{
do
{
if((fileinfo.attrib & _A_SUBDIR))
{
if(strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
{
//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
GetAllFormatFiles( p.assign(path).append("\\").append(fileinfo.name), files,format);
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
// 该函数有两个参数,第一个为路径字符串(string类型,最好为绝对路径);
// 第二个参数为文件夹与文件名称存储变量(vector类型,引用传递)。
// 在主函数中调用格式(并将结果保存在文件"AllFiles.txt"中,第一行为总数):
int main()
{
string filePath = "testimages\\water";
vector<string> files;
char * distAll = "AllFiles.txt";
//读取所有的文件,包括子文件的文件
//GetAllFiles(filePath, files);
//读取所有格式为jpg的文件
string format = ".jpg";
GetAllFormatFiles(filePath, files,format);
ofstream ofn(distAll);
int size = files.size();
ofn<<size<<endl;
for (int i = 0;i<size;i++)
{
ofn<<files[i]<<endl;
cout<< files[i] << endl;
}
ofn.close();
return 0;
}
相关文章推荐
- C++读取文件夹中所有的文件或者是特定后缀的文件
- C++读取文件夹中特定后缀的文件—dirent—vector
- C++读取当前文件夹下所有某种类型文件
- windows下,c /c++实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码
- C++获取文件夹下特定后缀的所有文件名
- MFC查找所有后缀文件获取文件大小读取特定位置数据
- OpenCV/C/C++读取文件夹内所有图或者多张图片
- c++读取某文件夹下的所有文件并查找.txt类型文件名
- Photoshop脚本之获得文件夹下所有特定后缀文件
- c++读取文件夹所有文件路径
- c++中 打开一个文件夹下的所有特定格式的文件(如JPG)
- c++ 读取文件夹下的所有文件名(实现未知文件名也可以批量读取文件)
- windows下,c /c++实现磁盘扫描,结合配置文件,读取特定后缀文件目录代码
- 算法积累:解决如何获取指定文件夹路径或者文件路径下所有子文件后缀为.h .m .c的文本的行数
- C++读取文件夹中所有文件的路径,包含子文件夹
- ubuntu C++ 读取文件夹下所有文件的文件名
- 算法积累:解决如何获取指定文件夹路径或者文件路径下所有子文件后缀为.h .m .c的文本的行数
- 基于C/C++的读取文件夹下所有文件(图片、文档等)的代码
- [003]从文件夹中读取所有图片或者文件
- C++文件与文件夹操作(2)--判断并筛选文件夹目录下特定后缀文件