读取文件夹里的图片,能够显示出图片的文件名和个数(不仅是图片,还可以是其他文件)

来源:互联网 发布:软件开发 模型 编辑:程序博客网 时间:2024/06/05 16:28

头文件

FilePath.h

#ifndef FILE_PATH_H

#define FILE_PATH_H

#include<io.h>

#include <unordered_map>

#include <vector>

using namespace std;

class FilePath{

public:

       FilePath();

       ~FilePath();

      void getFile(string path,vector<string>& files);

      int getFilesAll(string path,unordered_map<string,vector<string>>&files,int threshold=INT_MAX)

      int getFilesAll(string path,vector<string>&files,int threshold=INT_MAX);

}

#endif



源文件:

FilePath.cpp

FilePath::FilePath()

{

}


FilePath::~FilePath()

{

}

void FilePath::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,",")!=&&strcmp(fileinfo.name,"..")!=0)

                                    continue;

                      }

                      else

                       {

                               string fn(fileinfo.name);

                               if(fn.length()<4)     continue;

                               fn=fn.sunstr(fn.length()-4)

                               if(fn==".jpg" || fn=".png" || fn== ".bmp"  || fn=="jpeg")

                                       files.push_back(p.assign(path).append("\\").append(fileinfo.name))                    

                       }

                   }while(_findnext(hFile,&fileinfo)==0);

                   _findclose(hFile);

        }

}


//这个函数是实现:一个文件夹里包含多个子文件夹,子文件夹中包含多张照片

int FilePath::getFilesAll(string path,unordered_map<string ,vector<string>>&files,int threshold)

{

      //文件句柄

     long hFile=0;

     //文件信息

     struct   _finddata_t fileinfo;

      string p;

       int res=0;

        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)

                            {

                                     vector<string> pf;

                                     getFiles(path+"/"+fileinfo.name,pf);

                                     files.insert(make_pair(fileinfo.name,pf));

                                      res+=static_cast<int>(pf.size())>threshold:pf.size();

                             }

                      }

              }while(_findnext(hFile,&fileinfo)==0);

                _findclose(hFile);

        }

      return res;

}


//这个函数实现:一个文件夹中直接就是多张图片

int FilePath::getFilesAll(string path,vector<string>&files,int threshold  )

{

      //文件句柄

      long hFile=0;

      //文件信息

      struct  _finddata_t  fileinfo;

       string p;

        if((hFile= _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo))!=-1)

        {

               do

                  {

                         //如果是目录,迭代之

                         //如果不是,加入列表

                          iffileinfo.attrib & _A_SUBDIR)

                           {

                                    if(strcmp(fileinfo.name,".")!=0&&strcmp(fileinfo.name,"..")!=0)

                                            continue;

                           }

                           else

                            {

                                    string fn(fileinfo.name);

                                     if(fn.length()<4)

                                            continue;

                                      fn=fn.substr(fn.length() -4 );

                                      if(fn==".jpg" || fn==".png" || fn== ".bmp" || fn=="jpg")

                                           files.push_back(p.assign(path).append("\\").append(fileinfo.name));

                            }

                  }while (_findnext(hFile,&fileinfo)==0);

                  _findclose(hFile);

        }

        return   files.size();

}

0 0
原创粉丝点击