国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

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

2007.07.06

關注

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


    在windows下,可以使用FindFirstFile和FindNextFile來實現。

    而在Linux下,則可以使用opendir和readdir來實現。

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

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

#include <stddef.h>

#include <stdio.h>

#include <sys/types.h>

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

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


本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
VC C++ 遍歷指定目錄 Windows API
VC 遍歷指定目錄下的文件
Win32的時間類型
一段簡單的C/C++病毒源程序
在Windows下,如何遍歷某目錄里所有文件?
獲取目錄下所有文件名
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服