免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
遍歷讀取指定文件夾下指定類型的所有文件

經(jīng)常碰到朋友,尤其是初學(xué)者對(duì)指定文件夾下指定類型文件的讀取很困惑,這里,我把自己經(jīng)常用的程序貼出來,供初學(xué)者學(xué)些;

 

#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());//比較字符串a(chǎn)rg1 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;
}

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
一文讓你徹底明白C 中的const
C/C++的格式化輸出
C++引用參數(shù)傳遞錯(cuò)誤
c++ string 和wstring 之間的互相轉(zhuǎn)換函數(shù)
標(biāo)準(zhǔn)字符串std::string和System::String, CString之間的轉(zhuǎn)換
string與wstring轉(zhuǎn)換
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服