小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

遞歸遍歷某一路徑下的所有文件(for windows or linux)

 imzjw 2007-07-06

遞歸遍歷某一路徑下的所有文件


    在windows下,可以使用FindFirstFile和FindNextFile來實(shí)現(xiàn)。

    而在Linux下,則可以使用opendir和readdir來實(shí)現(xiàn)。

    具體實(shí)現(xiàn)見下面兩個(gè)函數(shù),分別實(shí)現(xiàn)了打印某一路徑下的所有
文件,包括子目錄下的文件。在具體實(shí)現(xiàn)的時(shí)候需要注意設(shè)置路徑。

注:
    下面兩個(gè)程序都通過編譯通過,且正常執(zhí)行。
    windows下使用VC6.0編譯;
    Linux下使用gcc 3.4.3編譯。

#include <stddef.h>

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>       // stat 函數(shù)所在的文件

#include <dirent.h>

//for windows
void findAllFile(char * pFilePath)
{
 
 WIN32_FIND_DATA FindFileData;
 HANDLE hFind = INVALID_HANDLE_VALUE;
 char DirSpec[MAX_PATH + 1];  // directory specification
 DWORD dwError;
 
 strncpy (DirSpec, pFilePath, strlen(pFilePath) + 1);
 SetCurrentDirectory(pFilePath);
 strncat (DirSpec, "\\*", 3);
 
 hFind = FindFirstFile(DirSpec, &FindFileData);
 
 if (hFind == INVALID_HANDLE_VALUE)
 {
  printf ("Invalid file handle. Error is %u\n", GetLastError());
  return ;
 }
 else
 {
  if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
  {
   printf ("    %s\n", FindFileData.cFileName);
  }
  else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
   && strcmp(FindFileData.cFileName, ".") != 0
   && strcmp(FindFileData.cFileName, "..") != 0)
  {
   char Dir[MAX_PATH + 1];
   strcpy(Dir, pFilePath);
   strncat(Dir, "\\", 2);
   strcat(Dir, FindFileData.cFileName);
   
   findAllFile(Dir);
  }
  
  while (FindNextFile(hFind, &FindFileData) != 0)
  {
   if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
   {
    printf ("    %s\n", FindFileData.cFileName);
   }
   else if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
    && strcmp(FindFileData.cFileName, ".") != 0
    && strcmp(FindFileData.cFileName, "..") != 0)
   {
    char Dir[MAX_PATH + 1];
    strcpy(Dir, pFilePath);
    strncat(Dir, "\\", 2);
    strcat(Dir, FindFileData.cFileName);
    findAllFile(Dir);
   }
   
  }
  
  dwError = GetLastError();
  FindClose(hFind);
  if (dwError != ERROR_NO_MORE_FILES)
  {
   printf ("FindNextFile error. Error is %u\n", dwError);
   return;
  }
 }
}


//for linux
void findAllFile(char * pFilePath)
{
 DIR * dir;
 dirent * ptr;
 struct stat stStatBuf;
 chdir(pFilePath);
        dir = opendir(pFilePath);
 while ((ptr = readdir(dir)) != NULL)
     {
  if (stat(ptr->d_name, &stStatBuf) == -1)
  {
   printf("Get the stat error on file:%s\n", ptr->d_name);
   continue;
  }
  if ((stStatBuf.st_mode & S_IFDIR) && strcmp(ptr->d_name, ".") != 0
    && strcmp(ptr->d_name, "..") != 0)
  {
   char Path[MAX_PATH];
   strcpy(Path, pFilePath);
   strncat(Path, "/", 1);
   strcat(Path, ptr->d_name);
   findAllFile(Path);
  }
  if (stStatBuf.st_mode & S_IFREG)
  {
   printf("  %s\n", ptr->d_name);
  }
  //this must change the directory , for maybe changed in the recured

function 
  chdir(pFilePath);
 }
     closedir(dir);
}

----------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <errno.h>
 /*for directroy funcgion */
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <fstream>

//#define _LINUX


#ifdef _LINUX
#include <sys/types.h>
#include <dirent.h>
#endif

#ifdef _WIN32
#include <windows.h>
#endif

using namespace std;

vector<string> ReadDir(string dirName);
void PrintVec(vector<string> vecStr);
int ProcessFile(string fileName);


int main(int argc ,char **argv)
{
 int i;
 string dirName;
 vector<string> strVec;
 

 if (argc == 1)
 {
  strVec = ReadDir(".");
 }else{
  strVec = ReadDir(argv[1]);
 }
 cout << "the size of strVec is " << strVec.size()<<endl;
 for(vector<string>::iterator pos =strVec.begin();pos<strVec.end();pos++)
  ProcessFile(*pos);
 return 0;
}

void PrintVec(vector<string> vecStr){
 for(int i=0;i<vecStr.size();i++)
  cout << vecStr[i]<<endl;
}
#ifdef _LINUX
vector<string> ReadDir(string dirName)
{
 DIR* dp;
 struct dirent* ent;
 vector<string> ret;
 
 
 if((dp=opendir(dirName.c_str())) == NULL){
 
  exit(1);
 }
 
 errno = 0;
 while((ent=readdir(dp))!= NULL){
  //cout << setw(8)<< ent->d_ino <<"    "<< ent->d_name << endl;
  ret.push_back(ent->d_name);
 
 }
 
 if(errno != 0){
 
  exit(1);
 }
 
 if(closedir(dp) !=0){
 
  exit(1);
 
 }
 return ret;
}
#endif

#ifdef _WIN32
vector<string> ReadDir(string dirName)
{
   char *fileName;
   char curDir[ 256 ];
   char fullName[ 256 ];
   HANDLE fileHandle;
   WIN32_FIND_DATA findData;
   vector<string> ret;
     
   // print out the current directory name
   if( !GetFullPathName("*.*", 256, fullName,&fileName ) )
      return ret;
   cout << endl << "Directory - " << fullName << endl;
     
   // Loop through all files in the directory
   fileHandle = FindFirstFile("*.*",&findData );
   while ( fileHandle != INVALID_HANDLE_VALUE )
   {
      // If the name is a directory,
      // recursively walk it. Otherwise
      // print the file‘s data
      if( findData.dwFileAttributes &
         FILE_ATTRIBUTE_DIRECTORY )
      {
         //ListDirectoryContents( findData.cFileName,fileMask );
      }
      else{
         ret.push_back(findData.cFileName);
      }       
     // loop thru remaining entries in the dir
      if (!FindNextFile( fileHandle, &findData ))
         break;
   }
  
   // clean up and restore directory
   FindClose( fileHandle );
   return ret;
}
#endif
 
int ProcessFile(string fileName)
{
 // open input file
 int counter =0;
 ifstream file (fileName.c_str());
 // file opened?
 if (! file) {
  // NO, abort program
  cerr << "can‘t open input file \"" << fileName << "\""<< endl;
  return (0);
 }

 char c;
 while (file.get(c)) {
  if( c == ‘\n‘){
   counter++;
  }
 }
 cout << fileName << "---->" << counter <<endl;
 return counter;
 
 
}
 


    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多