• <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>

            用VC++遍歷目錄

            ---- 所謂遍歷目錄,就是給定一個目錄,訪問其中的所有文件(包括子目錄下的文件)。迭代是比較常用的遍歷算法。本文利用C++面向?qū)ο蟮奶匦?,通過一個類CBrowseDir,對目錄遍歷進(jìn)行了封裝。用戶只需掌握該類四個成員函數(shù)的用法,就可以在自己的程序中,很方便地實(shí)現(xiàn)目錄遍歷。

            ---- 類CBrowseDir使用了迭代算法。因?yàn)樗惴ú皇潜疚闹攸c(diǎn),筆者不打算展開進(jìn)一步討論,對其感興趣者可參考相關(guān)資料。

            一、類成員函數(shù)說明:

            ---- bool SetInitDir(const char *dir);

            ---- 功能:設(shè)置要遍歷的目錄。

            ---- 參數(shù):dir 指向要遍歷的目錄,可以使用相對路徑,比如"d:..hawk";還可以使用網(wǎng)絡(luò)路徑,比如"\wfdhawk"(其中wf是主機(jī)名,d是共享目錄,hawk是目錄)。

            ---- 返回值:返回true,表示設(shè)置成功;返回false,說明目錄不可用。

            ---- bool BeginBrowse(const char *filespec);

            ---- 功能:開始遍歷目錄中由filespec指定的文件(包括隱藏文件)。

            ---- 參數(shù):filespec 指定文件類型,可以使用通配符*和?,比如"*.exe"或"a?.*"都是合法參數(shù)。注意:filespec中不能包含路徑,象"hawk*.*"是錯誤的。

            ---- 返回值:函數(shù)返回true,表明已順利遍歷完所有文件;返回false,遍歷過程被用戶中止。

            ---- virtual bool ProcessFile(const char *filename);

            ---- 功能:虛函數(shù)。每找到一個文件,程序就會調(diào)用ProcessFile,并把文件名作為參數(shù)傳遞給函數(shù)。如果函數(shù)返回false,則強(qiáng)制遍歷中止,并導(dǎo)致類成員函數(shù)函數(shù)BeginBrowse返回false。 用戶應(yīng)該覆寫此函數(shù),以加入自己的處理代碼。

            ---- 參數(shù):filename 指向一個文件名。注意:filename使用絕對路徑。

            ---- 返回值:返回true,繼續(xù)遍歷;否則,中止遍歷。

            ---- virtual void ProcessDir (const char *currentdir,const char *parentdir);

            ---- 功能:虛函數(shù)。在遍歷過程中,每進(jìn)入一個子目錄,程序就會調(diào)用ProcessDir,并把目錄名及其上一級目錄名作為參數(shù)傳遞給函數(shù)。如果該目錄是成員函數(shù)SetInitDir指定的初始目錄,則parentdir=NULL。用戶可以覆寫此函數(shù),以加入自己的處理代碼。比如可以在這里統(tǒng)計(jì)子目錄的個數(shù)。

            ---- 參數(shù):currentdir 指向一個子目錄。
            ---- parentdir 指向currentdir的父目錄。
            ---- 注意:currentdir和parentdir均使用絕對路徑。

            二、使用:

            ---- 把類CBrowseDir的頭文件BrowseDir.h及實(shí)現(xiàn)文件BrowseDir.cpp加到項(xiàng)目(Project)中,然后派生自己的類并覆寫虛函數(shù)ProcessFile和ProcessDir。遍歷目錄時,先構(gòu)造一個派生類對象,用成員函數(shù)SetInitDir指定目錄,然后調(diào)用BeginBrowse開始遍歷。

            ---- 本文提供了一個例子 example.cpp,它從CBrowseDir派生出子類CStatDir,通過統(tǒng)計(jì)函數(shù)ProcessFile及ProcessDir的調(diào)用次數(shù),可以得知目錄中的文件及子目錄個數(shù)。程序都有注釋,這里就不再羅嗦了。

            三、注意事項(xiàng):

            ---- 1. 類CBrowseDir會改變當(dāng)前工作目錄。同一個相對路徑,使用CBrowseDir前后,可能會有不同的含義。因此用戶編程時,要小心使用相對路徑。

            ---- 2. 如果項(xiàng)目(Project)是一個MFC應(yīng)用程序,直接加入BrowseDir.h及BrowseDir.cpp會導(dǎo)致編譯出錯。這是因?yàn)槿笔∏闆r下,MFC項(xiàng)目使用了預(yù)編譯頭(Precompiled Header),而BrowseDir.h和BrowseDir.cpp是用標(biāo)準(zhǔn)C++語句編寫的,沒用預(yù)編譯。一個解決辦法是先用類向?qū)深怌BrowseDir的"架子",再把相應(yīng)的代碼拷貝過去。

            ---- 本文代碼均在Win95、Visual C++ 5.0環(huán)境下調(diào)試通過。

            附源代碼:

            /**************************************************
                這是CBrowseDir的類定義文件 BrowseDir.h

            /**************************************************
            #include "stdlib.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每進(jìn)入一個目錄,就調(diào)用ProcessDir
            //并把正在處理的目錄名及上一級目錄名作為參數(shù)傳遞過去
            //如果正在處理的是初始目錄,則parentdir=NULL
            //用戶可以覆寫該函數(shù),加入自己的處理代碼
            //比如用戶可以在這里統(tǒng)計(jì)子目錄的個數(shù)
            virtual void ProcessDir(const char
            *currentdir,const char *parentdir);
            };


            /*********************************************/

            這是CBrowseDir的類實(shí)現(xiàn)文件 BrowseDir.cpp

            /***********************************************/
            #include "stdlib.h"
            #include "direct.h"
            #include "string.h"
            #include "io.h"

            #include "browsedir.h"

            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
            {
            //檢查是不是目錄
            //如果不是,則進(jìn)行處理
            if (!(fileinfo.attrib & _A_SUBDIR))
            {
            char filename[_MAX_PATH];
            strcpy(filename,dir);
            strcat(filename,fileinfo.name);
            if (!ProcessFile(filename))
            return false;
            }
            } while (_findnext(hFile,&fileinfo) == 0);
            _findclose(hFile);
            }

            //查找dir中的子目錄
            //因?yàn)樵谔幚韉ir中的文件時,派生類的ProcessFile有可能改變了
            //當(dāng)前目錄,因此還要重新設(shè)置當(dāng)前目錄為dir。
            //執(zhí)行過_findfirst后,可能系統(tǒng)記錄下了相關(guān)信息,因此改變目錄
            //對_findnext沒有影響。
            _chdir(dir);
            if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
            {
            do
            {
            //檢查是不是目錄
            //如果是,再檢查是不是 . 或 ..
            //如果不是,進(jìn)行迭代
            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)
            {
            }


            /*************************************************
            這是例子example.cpp
              
            /*************************************************
            #include "stdio.h"

            #include "BrowseDir.h"

            //從CBrowseDir派生出的子類,用來統(tǒng)計(jì)目錄中的文件及子目錄個數(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()
            {
            //因?yàn)檫M(jìn)入初始目錄時,也會調(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)計(jì)的目錄名:");
            gets(buf);

            //構(gòu)造類對象
            CStatDir statdir;

            //設(shè)置要遍歷的目錄
            if (!statdir.SetInitDir(buf))
            {
            puts("目錄不存在。");
            return;
            }

            //開始遍歷
            statdir.BeginBrowse("*.*");

            //統(tǒng)計(jì)結(jié)果中,子目錄個數(shù)不含 . 及 ..
            printf("文件總數(shù): %d 子目錄總數(shù):
            %d ",statdir.GetFileCount(),
            statdir.GetSubdirCount());
            }  

            posted on 2010-06-21 22:55 lhking 閱讀(1332) 評論(2)  編輯 收藏 引用

            評論

            # re: 用VC++遍歷目錄 2011-10-16 09:42 yuhang85

            您好 ,請教下啊,為什么VC的MSDN還有VS2008的MSDN里面 都沒有CBrowseDir類呢???你說的這個類是自己寫的還是微軟提供的呢??您用的環(huán)境是VC嗎???  回復(fù)  更多評論   

            # re: 用VC++遍歷目錄 2014-03-28 17:30 MistySoul

            @yuhang85
            顯然是自己寫的啊  回復(fù)  更多評論   


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


            導(dǎo)航

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            91久久精品电影| 久久精品国产亚洲AV大全| 久久人妻少妇嫩草AV蜜桃| 国产精品亚洲综合专区片高清久久久| 久久国产影院| 久久精品www人人爽人人| 久久91精品综合国产首页| 熟妇人妻久久中文字幕| 精品久久综合1区2区3区激情 | 久久久精品日本一区二区三区| 少妇无套内谢久久久久| 久久九色综合九色99伊人| 狼狼综合久久久久综合网| 亚洲美日韩Av中文字幕无码久久久妻妇 | 国产亚洲色婷婷久久99精品91| 无码国内精品久久人妻| 久久影院午夜理论片无码| 国产一级做a爰片久久毛片| 东方aⅴ免费观看久久av| 久久久久国产精品嫩草影院| 精品久久久久久无码免费| 91亚洲国产成人久久精品网址| 久久国产精品99国产精| 乱亲女H秽乱长久久久| 熟妇人妻久久中文字幕| 亚洲国产精品成人久久| 亚洲国产小视频精品久久久三级 | 欧美日韩久久中文字幕| 久久久久亚洲AV成人网| 久久国产乱子伦精品免费强| 国产69精品久久久久777| 久久99精品国产| 久久精品男人影院| 97久久精品人人做人人爽| 九九热久久免费视频| 亚洲а∨天堂久久精品| 午夜精品久久久久久| 无码精品久久久天天影视| 久久精品国产福利国产秒| 国产高清国内精品福利99久久| 久久精品国产色蜜蜜麻豆 |