对指定文件夹下指定类型文件的读取

转载地址:http://blog.csdn.net/carson2005/article/details/6292726

Matlab中顺序读取文件图片代码

function fileList = readFileListFromFolder(folderName, fileSuffix)

% Read all the files with special suffix to a file
% list.
% A simple use case: 
% fileList = readFileListFromFolder('test\', 'txt');

%Check the input folderName
if ~isempty(findstr(folderName,' '))
disp('There are some space in the folder name: ');
disp([' ' folderName]);
disp('Please change to use the version2 instead of this one!'); 
end

if strcmp(folderName(end), '\')
[s, w] = dos(['dir ' folderName '*.' fileSuffix ' /s /B >fileList.txt']);
else
[s, w] = dos(['dir ' folderName '\*.' fileSuffix ' /s /B >fileList.txt']);
end

fileList = importdata('fileList.txt');

dos('del fileList.txt');

end


C++

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值