c++遍历文件夹内所有文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wolf2345/article/details/81709928

#include<vector>
#include<string>
#include<io.h>
#include<iostream>

using namespace std;
char * filePath = "D:\\JPEGImages";

void getFiles(string path, vector<string>& files)
{
    //文件句柄  
    long long hFile = 0;//这个地方需要特别注意,win10用户必须用long long 类型,win7可以用long类型
    //文件信息  
    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);
    }
}

void main()
{
    
    vector<string> files;

    ////获取该路径下的所有文件  
    getFiles(filePath, files);

    char str[30];
    int size = files.size();
    for (int i = 0; i < size; i++)
    {
        cout << files[i].c_str() << endl;
    }
}
 

猜你喜欢

转载自blog.csdn.net/wolf2345/article/details/81709928