C++遍历文件夹下的子目录和文件

180it 2020-10-08 PM 2398℃ 0条
#include <io.h>
#include <stdio.h>
 
void getFiles(string path, vector<string>& files, string postfix)
{
    //文件句柄    
    long   hFile = 0;
    //文件信息    
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = _findfirst(p.assign(path).c_str(), &fileinfo)) != -1)
    {
        do
        {
            //如果是目录,迭代之    
            //如果不是,加入列表    
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("\\*"), files, postfix);
            }
            else
            {
                 if (postfix.size() == 0 || (postfix.size() > 0 && (postfix.c_str(), strchr(fileinfo.name, '.'))))
                    files.push_back(p.assign(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}
 
void main()
{
    vector<string> files;
    getFiles("testvideo", files, ".jpg|.avi");
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C++遍历文件夹下的子目录和文件