• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 224  文章 - 41  trackbacks - 0
            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

            經(jīng)典c++博客

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            原文地址:http://blog.csdn.net/zhangzuohai/archive/2009/06/19/4282117.aspx
            遍歷所有的文件和文件夾并對文件進行操作。

            #include "stdlib.h"   #include "direct.h"   #include "string.h"   #include "io.h"   #include "stdio.h"    #include "iostream.h"     class CBrowseDir   {   protected:       //存放初始目錄的絕對路徑,以'\'結(jié)尾       char m_szInitDir[_MAX_PATH];          public:       //缺省構(gòu)造器       CBrowseDir();              //設(shè)置初始目錄為dir,如果返回false,表示目錄不可用       bool SetInitDir(const char *dir);              //開始遍歷初始目錄及其子目錄下由filespec指定類型的文件       //filespec可以使用通配符 * ?,不能包含路徑。       //如果返回false,表示遍歷過程被用戶中止       bool BeginBrowse(const char *filespec);          protected:       //遍歷目錄dir下由filespec指定的文件       //對于子目錄,采用迭代的方法       //如果返回false,表示中止遍歷文件       bool BrowseDir(const char *dir,const char *filespec);              //函數(shù)BrowseDir每找到一個文件,就調(diào)用ProcessFile       //并把文件名作為參數(shù)傳遞過去       //如果返回false,表示中止遍歷文件       //用戶可以覆寫該函數(shù),加入自己的處理代碼       virtual bool ProcessFile(const char *filename);              //函數(shù)BrowseDir每進入一個目錄,就調(diào)用ProcessDir       //并把正在處理的目錄名及上一級目錄名作為參數(shù)傳遞過去       //如果正在處理的是初始目錄,則parentdir=NULL       //用戶可以覆寫該函數(shù),加入自己的處理代碼       //比如用戶可以在這里統(tǒng)計子目錄的個數(shù)       virtual void ProcessDir(const char *currentdir,const char *parentdir);   };     CBrowseDir::CBrowseDir()   {       //用當(dāng)前目錄初始化m_szInitDir       getcwd(m_szInitDir,_MAX_PATH);              //如果目錄的最后一個字母不是'\',則在最后加上一個'\'       int len=strlen(m_szInitDir);       if (m_szInitDir[len-1] != '\\')           strcat(m_szInitDir,"\\");   }     bool CBrowseDir::SetInitDir(const char *dir)   {       //先把dir轉(zhuǎn)換為絕對路徑       if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)           return false;              //判斷目錄是否存在       if (_chdir(m_szInitDir) != 0)           return false;              //如果目錄的最后一個字母不是'\',則在最后加上一個'\'       int len=strlen(m_szInitDir);       if (m_szInitDir[len-1] != '\\')           strcat(m_szInitDir,"\\");              return true;   }     bool CBrowseDir::BeginBrowse(const char *filespec)   {       ProcessDir(m_szInitDir,NULL);       return BrowseDir(m_szInitDir,filespec);   }     bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)   {       _chdir(dir);              //首先查找dir中符合要求的文件       long hFile;       _finddata_t fileinfo;       if ((hFile=_findfirst(filespec,&fileinfo)) != -1)       {           do          {               //檢查是不是目錄               //如果不是,則進行處理               if (!(fileinfo.attrib & _A_SUBDIR))               {                   char filename[_MAX_PATH];                   strcpy(filename,dir);                   strcat(filename,fileinfo.name);                   cout << filename << endl;                   if (!ProcessFile(filename))                       return false;               }           } while (_findnext(hFile,&fileinfo) == 0);           _findclose(hFile);       }       //查找dir中的子目錄       //因為在處理dir中的文件時,派生類的ProcessFile有可能改變了       //當(dāng)前目錄,因此還要重新設(shè)置當(dāng)前目錄為dir。       //執(zhí)行過_findfirst后,可能系統(tǒng)記錄下了相關(guān)信息,因此改變目錄       //對_findnext沒有影響。       _chdir(dir);       if ((hFile=_findfirst("*.*",&fileinfo)) != -1)       {           do          {               //檢查是不是目錄               //如果是,再檢查是不是 . 或 ..                //如果不是,進行迭代               if ((fileinfo.attrib & _A_SUBDIR))               {                   if (strcmp(fileinfo.name,".") != 0 && strcmp                       (fileinfo.name,"..") != 0)                   {                       char subdir[_MAX_PATH];                       strcpy(subdir,dir);                       strcat(subdir,fileinfo.name);                       strcat(subdir,"\\");                       ProcessDir(subdir,dir);                       if (!BrowseDir(subdir,filespec))                           return false;                   }               }           } while (_findnext(hFile,&fileinfo) == 0);           _findclose(hFile);       }       return true;   }     bool CBrowseDir::ProcessFile(const char *filename)   {       return true;   }     void CBrowseDir::ProcessDir(const char                                *currentdir,const char *parentdir)   {   }     //從CBrowseDir派生出的子類,用來統(tǒng)計目錄中的文件及子目錄個數(shù)   class CStatDir:public CBrowseDir   {   protected:       int m_nFileCount;   //保存文件個數(shù)       int m_nSubdirCount; //保存子目錄個數(shù)          public:       //缺省構(gòu)造器       CStatDir()       {           //初始化數(shù)據(jù)成員m_nFileCount和m_nSubdirCount           m_nFileCount=m_nSubdirCount=0;       }              //返回文件個數(shù)       int GetFileCount()       {           return m_nFileCount;       }              //返回子目錄個數(shù)       int GetSubdirCount()       {           //因為進入初始目錄時,也會調(diào)用函數(shù)ProcessDir,           //所以減1后才是真正的子目錄個數(shù)。           return m_nSubdirCount-1;       }          protected:       //覆寫虛函數(shù)ProcessFile,每調(diào)用一次,文件個數(shù)加1       virtual bool ProcessFile(const char *filename)       {           m_nFileCount++;           return CBrowseDir::ProcessFile(filename);       }              //覆寫虛函數(shù)ProcessDir,每調(diào)用一次,子目錄個數(shù)加1       virtual void ProcessDir           (const char *currentdir,const char *parentdir)       {           m_nSubdirCount++;           CBrowseDir::ProcessDir(currentdir,parentdir);       }   };     void main()   {       //獲取目錄名       char buf[256];       printf("請輸入要統(tǒng)計的目錄名:");       gets(buf);              //構(gòu)造類對象       CStatDir statdir;              //設(shè)置要遍歷的目錄       if (!statdir.SetInitDir(buf))       {           puts("目錄不存在。");           return;       }              //開始遍歷       statdir.BeginBrowse("*.*");       printf("文件總數(shù): %d\n子目錄總數(shù):%d\n",statdir.GetFileCount(),statdir.GetSubdirCount());   }   

            本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http:
            //blog.csdn.net/zhangzuohai/archive/2009/06/19/4282117.aspx

            相關(guān)函數(shù)的解釋
            --------------------------------------------------------------------------------
            _fullpath(char*abspath,char* relpath,size_t maxsize);
            參數(shù)解釋

            absPath
            Pointer to a buffer containing the absolute or full path name, or NULL.

            relPath
            Relative path name.

            maxLength
            Maximum length of the absolute path name buffer (absPath ). This length is in bytes for _fullpath but in wide characters (wchar_t ) for _wfullpath .

            返回值
            Each of these functions returns a pointer to a buffer containing the absolute path name (absPath ). If there is an error (for example, if the value passed in relPath includes a drive letter that is not valid or cannot be found, or if the length of the created absolute path name (absPath ) is greater than maxLength ), the function returns NULL .

              Remarks

            The _fullpath function expands the relative path name in relPath to its fully qualified or absolute path and stores this name in absPath . If absPath is NULL, malloc is used to allocate a buffer of sufficient length to hold the path name. It is the responsibility of the caller to free this buffer. A relative path name specifies a path to another location from the current location (such as the current working directory: "."). An absolute path name is the expansion of a relative path name that states the entire path required to reach the desired location from the root of the file system. Unlike _makepath , _fullpath can be used to obtain the absolute path name for relative paths (relPath ) that include "./" or "../" in their names.

            For example, to use C run-time routines, the application must include the header files that contain the declarations for the routines. Each header file include statement references the location of the file in a relative manner (from the application's working directory):

            --------------------------------------------------------------------------------
            改變當(dāng)前的工作目錄到指定的目錄中
            _chdir, _wchdir
            Changes the current working directory.

            int _chdir(
               const char *dirname
            );
            int _wchdir(
               const wchar_t *dirname
            );Parameters
            dirname
            Path of new working directory.

              Return Value
            These functions return a value of 0 if successful. A return value of –1 indicates failure. If the specified path could not be found, errno is set to ENOENT . If dirname is NULL, the invalid parameter handler is invoked, as described in Parameter Validation . If execution is allowed to continue, errno is set to EINVAL and the function returns -1.

              Remarks

            The _chdir function changes the current working directory to the directory specified by dirname . The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive. If a new drive letter is specified in dirname , the default drive letter is changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive:

            代碼

            posted on 2009-12-13 14:40 漂漂 閱讀(1084) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            www.久久99| 久久狠狠爱亚洲综合影院| 91久久精品国产免费直播| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 97久久精品午夜一区二区| 久久99国产精品久久99| 久久婷婷五月综合色99啪ak| 欧美精品九九99久久在观看| 国内精品久久国产大陆| 亚洲人成无码网站久久99热国产 | 久久久久亚洲av无码专区喷水 | 久久综合丁香激情久久| 久久久久av无码免费网| 狠狠色丁香婷婷综合久久来来去| 久久中文字幕人妻熟av女| 99热热久久这里只有精品68| 久久久亚洲AV波多野结衣| 久久久久国产精品麻豆AR影院| 久久66热人妻偷产精品9| 日本WV一本一道久久香蕉| 久久久艹| 日韩十八禁一区二区久久| 久久久久中文字幕| 久久婷婷国产麻豆91天堂| 91久久精一区二区三区大全| 区久久AAA片69亚洲| 中文字幕精品久久| 久久久久亚洲AV成人网人人软件| 久久99精品国产麻豆宅宅| 99国产欧美久久久精品蜜芽| 亚洲中文字幕无码久久精品1 | 无码人妻久久一区二区三区蜜桃| 99久久精品国产高清一区二区| 日韩久久久久久中文人妻| 国产69精品久久久久9999APGF | 色综合久久中文字幕无码| 久久久亚洲欧洲日产国码是AV| 久久综合偷偷噜噜噜色| 模特私拍国产精品久久| 少妇久久久久久被弄到高潮 | 久久综合亚洲色HEZYO国产|