zoukankan      html  css  js  c++  java
  • 遍历读取指定文件夹下指定类型的所有文件

    经常碰到朋友,尤其是初学者对指定文件夹下指定类型文件的读取很困惑,这里,我把自己经常用的程序贴出来,供初学者学些;

    #include "stdafx.h"
    #include "windows.h"
    #include <vector>
    #include <string>
    #include "iostream"
    using namespace std;
    typedef std::vector<std::string> file_lists;

    static int str_compare(const void *arg1, const void *arg2)
    {
           return strcmp((*(std::string*)arg1).c_str(), (*(std::string*)arg2).c_str());//比较字符串arg1 and arg2
    }

    file_lists ScanDirectory(const std::string &path, const std::string &extension)
    {
        WIN32_FIND_DATA wfd;//WIN32_FIND_DATA:Contains information about the file that is found by the 
            //FindFirstFile, FindFirstFileEx, or FindNextFile function
         HANDLE hHandle;
         string searchPath, searchFile;
         file_lists vFilenames;
         int nbFiles = 0;
        
         searchPath = path + "/*" + extension;
         hHandle = FindFirstFile(searchPath.c_str(), &wfd);//Searches a directory for a file or subdirectory
                  //with a name that matches a specific name
         if (INVALID_HANDLE_VALUE == hHandle)
        {
             fprintf(stderr, "ERROR(%s, %d): Cannot find (*.%s)files in directory %s/n",
                  __FILE__, __LINE__, extension.c_str(), path.c_str());
             exit(0);
        }
        do
        {
             //. or ..
              if (wfd.cFileName[0] == '.')
             {
                  continue;
              }
              // if exists sub-directory
              if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//dwFileAttributes:The file attributes of a file
            {             

              //FILE_ATTRIBUTE_DIRECTORY:The handle identifies a directory
                 continue;
             }
            else//if file
            {
                searchFile = path + "/" + wfd.cFileName;
                vFilenames.push_back(searchFile);
                nbFiles++;
             }
        }while (FindNextFile(hHandle, &wfd));//Call this member function to continue a file search begun 
              //with a call to CGopherFileFind::FindFile

        FindClose(hHandle);//Closes a file search handle opened by the FindFirstFile, FindFirstFileEx,
           //or FindFirstStreamW function

     // sort the filenames
        qsort((void *)&(vFilenames[0]), (size_t)nbFiles, sizeof(string), str_compare);//Performs a quick sort

        return vFilenames;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
         file_lists files = ScanDirectory("D://PicturesForTestInTheHall//TestGroup5", ".jpg");
         if (files.empty())
         {
              cout<<"no image file find in current directory.."<<endl;
              system("pause");
              exit(-1);
          }

          int size = files.size();
          cout<<"there are "<<size<<" image files totally...."<<endl;
          for (int i=0; i<size; i++)
         {
               cout<<files[i].c_str()<<endl;
          }

          system("pause");
          return 0;
    }

  • 相关阅读:
    SqlServer禁用启用触发器、外键约束
    解决Windows Server2008R2中导入Excel不能使用Jet 4.0
    C#读写配置文件
    C#后台如何获取客户端访问系统型号
    Flex在Win10,Chrome浏览器上汉字乱码的问题
    flex lineChart 显示所有的数据节点
    HTTP状态码大全
    zabbix使用sendEmail发送邮件报警
    zabbix安装
    Samba服务器搭建配置
  • 原文地址:https://www.cnblogs.com/yingying0907/p/2126084.html
Copyright © 2011-2022 走看看