C++ 获取文件下的所有文件的名字

来源:互联网 发布:大数据开发培训课程 编辑:程序博客网 时间:2024/05/24 03:22
#include<iostream>#include<opencv2\opencv.hpp>#include<string>#include<vector>using namespace std;using namespace cv;void getFiles(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)                    getFiles(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);    }}int main(){    string imgfile = "C:\\Users\\DP\\Desktop\\xiao-yolo\\Detector\\image";    vector<string>filename;    getFiles(imgfile, filename);    for(int i=0;i<filename.size();i++){        cout<<filename.at(i)<<endl;    }    return 0;}

win10下面的代码如下:

#include<iostream>#include<opencv2\opencv.hpp>#include<string>#include<vector>using namespace std;using namespace cv;void getFiles(string path, vector<string>& files){    //文件句柄      intptr_t  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)                    getFiles(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);    }}int main(){    string imgfile = "C:\\Users\\DP\\Desktop\\xiao-yolo\\Detector\\image";    vector<string>filename;    getFiles(imgfile, filename);    for(int i=0;i<filename.size();i++){        cout<<filename.at(i)<<endl;    }    return 0;}
0 0