搜索
查看: 1251|回复: 0
打印 上一主题 下一主题

遍历一个文件夹

[复制链接]
跳转到指定楼层
楼主
发表于 2013-3-3 10:30:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

linux下:

#include <stdlib.h>

#include <stdio.h>

#include <dirent.h>

#include <unistd.h>

#include <sys/stat.h>

int main(int argc, char** argv)

{

    DIR* dst;

    struct dirent *entry;

    struct stat info;

    if (argc != 2)

    {

        fprintf(stderr, "There must be 2 arguments");

        exit(EXIT_FAILURE);

    }

    if ((dst = opendir(argv[1])) == NULL)

    {

        perror("Fail to open dir");

        return -1;

    }/*get dst dir entry*/

    if(chdir(argv[1]) == -1)

    {

        perror("Fail while changing directory");

        return -1;

    }/*change cwd to dst dir*/

    while ((entry = readdir(dst)) != NULL)

    {/*reading all the file information*/

        if(stat(entry->d_name,&info) == -1)

        {

            perror("Fail to get the file stat");

            continue;

        }/*getting file state*/

        if(S_ISREG(info.st_mode))

            printf("%s\n",entry->d_name);

    }

    return 0;

}

c++


#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 %u\n",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;

}


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表