用C++遍历一个文件夹下面所有的文件 Zz

对指定的文件夹,遍历其内部所有的文件;
 
比较简单的办法就是在main()里面
system("dir c:\WT2G\ /B >filelist.txt");
 
下面这种方法也不错,需要在VC7.1及其以后编译;
 
  // findinfile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;
 
#pragma once
// 使用UNICODE字符集
#ifndef UNICODE
#define UNICODE
#endif
// 如果不使用Unicode可以解锁下面的:
//#undef UNICODE
//#undef _UNICODE
#include <windows.h>
#include <string>
#include <vector>
// 版本控制
// 因为我不知道VC7.1以前的版本是否有tchar.h
#if _MSC_VER < 1310
#error "注意,请您使用VC7.1或者以上版本编译此程序"
#endif
#include <tchar.h>
#pragma message("请注意:如果您的文件夹下面有中文文件名的文件的话,最好使用UNICODE字符集")
// 提供字符串的定义有中文的时候最好使用wstring
#ifdef UNICODE
typedef std::wstring String;
#else
typedef std::string String;
#endif
// 定义文件名的定义
typedef std::vector<String> FilesVec;
// 查找当前目录下的所有文件返回所有文件的文件名请以FilesVec的引用的方式传递
// pathName 为路径名
void FindFiles( const TCHAR * pathName ,FilesVec &files )
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   TCHAR PathBuffer[ _MAX_PATH ];
  
   _tcscpy_s( PathBuffer,_MAX_PATH, pathName );
   _tcscat_s( PathBuffer,_MAX_PATH, _T("\*") );
  
   hFind = FindFirstFile( PathBuffer ,&FindFileData );
   if( INVALID_HANDLE_VALUE == hFind )      // 如果出现了某种异常就直接抛出便可
   {
      char buffer[56];
      sprintf_s( buffer,"Invalid File Handle.Error is %un",GetLastError() );
      throw std::exception( buffer );
   }
   else // 然后再接着查找下去
   {
      files.push_back( String(FindFileData.cFileName) );  
     
      while( 0 != FindNextFile( hFind,&FindFileData ) )
      {
         files.push_back( String(FindFileData.cFileName) );
      }
      FindClose( hFind );
   }
}
 
int main()
{
FilesVec files;
try
{
   FindFiles( _T("C:\Program Files\") , files );
}
catch( exception &e )
{
   cout<<"查找过程中发生了异常"<<endl;
   cout<<e.what()<<endl;
   return -1;
}
#ifdef UNICODE
locale loc("chs");
wcout.imbue( loc );
copy( files.begin(), files.end(), ostream_iterator< String,TCHAR >( wcout, _T("n") ) );
#else
copy( files.begin(), files.end(), ostream_iterator< String,TCHAR >( cout, _T("n") ) );
#endif
system("pause");
return 0;
}
 
 
 
 

1、简介

  文件查找在很多场合会被派上用场,类ffsco将文件查找操作简单封装,使用只需要传递参数查找路径和文件匹配格式(可以继承该类的 match 方法实现自己的匹配算法)到find方法,查询结果(文件/目录等)被保存到类内部的vector容器,以后想怎么用都行。

要求:

  • 系统支持标准 C++/vector/string
  • 使用者了解 vector/string     
  • 2、说明

    使用WIN32提供的函数FindFirstFile/FindNextFile实现。子目录查找用递归方法。      3、使用方法

    将文件ffsco.h/ffsco.cpp加入,在使用的地方包含文件ffsco.h,e.g.

    #include "ffsco.h"

    加入名字空间使用声明:

    using namespace helper_coffs;

    定义ffsco类对象:

    ffsco o;

    设置是否查找子目录:

    //o.dirs(1);	//--查找子目录

    设置查找结果个数上限(默认65536/最大1048576当然可以自己修改限制):

    //o.limit(100);	//--最多查找100个

    开始查找(返回结果个数):

    int count = o.find(path, fext);
    //int count = o.find("c:\winnt");
    //int count = o.find("c:\winnt", "*.exe; *.dll; *.ini");
    //int count = o.find("c:\winnt\", "*.exe; *.dll; *.ini");

    取结果:

    ffsco::typeT coo;
    coo = o.co();	//--文件目录混合
    coo = o.co_dir();	//--全部目录
    coo = o.co_file();	//--全部文件

    使用结果:

    for (ffsco::typeT::iterator it = coo.begin(); coo.end() != it; it ++)
    {
    	cout << *it << endl;
    	//想怎么用???
    }

    就是这么简单:)

    更多使用请参考ffsco类提供的test()…    

     

    About superjiju

    Social Media, Sentiment Analysis, NLP, Information retrieval..
    This entry was posted in Cpp Programming. Bookmark the permalink.

    Leave a comment