由于经常有读取一个文件夹中的很多随机编号的文件,很多时候需要读取某些特定格式的所有文件。
下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件
参考:http://www.2cto.com/kf/201407/316515.html
windows平台代码:
- #include <io.h>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <iostream>
- 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;
- }
Linux平台代码:
- //LINUX/UNIX c获取某个目录下的所有文件的文件名
- #include <stdio.h>
- #include <dirent.h>
- int main(int argc, char * argv[])
- {
- struct dirent *ptr;
- DIR *dir;
- dir=opendir("./file");
- printf("文件列表:\n");
- while((ptr=readdir(dir))!=NULL)
- {
- //跳过'.'和'..'两个目录
- if(ptr->d_name[0] == '.')
- continue;
- printf("%s\n",ptr->d_name);
- }
- closedir(dir);
- return 0;
- }
C++ 版本
- #include <iostream>
- #include <vector>
- #include <string>
- #include <dirent.h>
- using namespace std;
- int main(int argc, char * argv[])
- {
- struct dirent *ptr;
- DIR *dir;
- string PATH = "./file";
- dir=opendir(PATH.c_str());
- vector<string> files;
- cout << "文件列表: "<< endl;
- while((ptr=readdir(dir))!=NULL)
- {
- //跳过'.'和'..'两个目录
- if(ptr->d_name[0] == '.')
- continue;
- //cout << ptr->d_name << endl;
- files.push_back(ptr->d_name);
- }
- for (int i = 0; i < files.size(); ++i)
- {
- cout << files[i] << endl;
- }
- closedir(dir);
- return 0;
- }