遍历一个文件夹下面的文件(半原创)

来源:互联网 发布:iphonex监控预约软件 编辑:程序博客网 时间:2024/04/28 00:52


#include <io.h>

#include <stdio.h>

#include <string>

#include <vector>

#include <iostream>

using namespace std;

int g_FileCounts = 0;

bool getAllFiles(const char *path, vector<string> vecFiles);  //函数声明


int main()

{

        string strFolderPath = "D:\\test\\" ;   //文件夹的路径

        string strFileFormat = "*.bmp";      //这里假设要得到.bmp格式的文件,所有文件为*.*,但是这样的话,会有两个点“.”和“..”,不知原因

        string strFilePath = strFolder + strFileFormat;

         const char *chFilePath = strFilePath.c_str();   //这是一种简单的转换方法string到char*,必须要带const

         vector<string>   vecFilePath;  //将路径下的文件存储到集合中去,这样的话,就可以通过读取集合得到所有的文件

         bool bRslt = getAllFiles(chFilePath, vecFilePath);


        return 0;

}

//得到路径下的指定文件

bool getAllFiles(const char *path, vector<string> vecFiles)

{

         //setup 

        _finddata_t    _fileData;

        long    lFile;

        if((lFile = _findfirst(path, &_fileData)) == -1)

        {

                           cout<<"No Files"<<endl;

                           return false;

         }

        else

         {

                              cout<<"File List:"<<endl;

                              do

{

int  nFileCounts = ++g_FileCounts ;

cout<<"第 "<<nFileCounts<<"个文件:"<<_fileData.name<<endl; //在命令窗口上观察

vecFiles.push_back(_fileData.name);

}while(_findnext(lFile, &_fileData) == 0)

}

//free

_findclose(lFile);

//return

return true;

}




0 0