• <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++遍歷目錄

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            二、使用:

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

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

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

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

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

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

            附源代碼:

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

            /**************************************************
            #include "stdlib.h"

            class CBrowseDir
            {
            protected:
            //存放初始目錄的絕對(duì)路徑,以結(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指定的文件
            //對(duì)于子目錄,采用迭代的方法
            //如果返回false,表示中止遍歷文件
            bool BrowseDir(const char *dir,const char *filespec);

            //函數(shù)BrowseDir每找到一個(gè)文件,就調(diào)用ProcessFile
            //并把文件名作為參數(shù)傳遞過去
            //如果返回false,表示中止遍歷文件
            //用戶可以覆寫該函數(shù),加入自己的處理代碼
            virtual bool ProcessFile(const char *filename);

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

            //如果目錄的最后一個(gè)字母不是,則在最后加上一個(gè)
            int len=strlen(m_szInitDir);
            if (m_szInitDir[len-1] != \)
            strcat(m_szInitDir,"\");
            }

            bool CBrowseDir::SetInitDir(const char *dir)
            {
            //先把dir轉(zhuǎn)換為絕對(duì)路徑
            if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)
            return false;

            //判斷目錄是否存在
            if (_chdir(m_szInitDir) != 0)
            return false;

            //如果目錄的最后一個(gè)字母不是,則在最后加上一個(gè)
            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中的文件時(shí),派生類的ProcessFile有可能改變了
            //當(dāng)前目錄,因此還要重新設(shè)置當(dāng)前目錄為dir。
            //執(zhí)行過_findfirst后,可能系統(tǒng)記錄下了相關(guān)信息,因此改變目錄
            //對(duì)_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派生出的子類,用來(lái)統(tǒng)計(jì)目錄中的文件及子目錄個(gè)數(shù)
            class CStatDir:public CBrowseDir
            {
            protected:
            int m_nFileCount;    //保存文件個(gè)數(shù)
            int m_nSubdirCount; //保存子目錄個(gè)數(shù)

            public:
            //缺省構(gòu)造器
            CStatDir()
            {
            //初始化數(shù)據(jù)成員m_nFileCount和m_nSubdirCount
            m_nFileCount=m_nSubdirCount=0;
            }

            //返回文件個(gè)數(shù)
            int GetFileCount()
            {
            return m_nFileCount;
            }

            //返回子目錄個(gè)數(shù)
            int GetSubdirCount()
            {
            //因?yàn)檫M(jìn)入初始目錄時(shí),也會(huì)調(diào)用函數(shù)ProcessDir,
            //所以減1后才是真正的子目錄個(gè)數(shù)。
            return m_nSubdirCount-1;
            }

            protected:
            //覆寫虛函數(shù)ProcessFile,每調(diào)用一次,文件個(gè)數(shù)加1
            virtual bool ProcessFile(const char *filename)
            {
            m_nFileCount++;
            return CBrowseDir::ProcessFile(filename);
            }

            //覆寫虛函數(shù)ProcessDir,每調(diào)用一次,子目錄個(gè)數(shù)加1
            virtual void ProcessDir
            (const char *currentdir,const char *parentdir)
            {
            m_nSubdirCount++;
            CBrowseDir::ProcessDir(currentdir,parentdir);
            }
            };

            void main()
            {
            //獲取目錄名
            char buf[256];
            printf("請(qǐng)輸入要統(tǒng)計(jì)的目錄名:");
            gets(buf);

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

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

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

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

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

            評(píng)論

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

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

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

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


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


            導(dǎo)航

            <2010年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            伊人情人综合成人久久网小说 | 亚洲AV日韩精品久久久久| 久久久久亚洲?V成人无码| 国产精品伦理久久久久久| 婷婷久久五月天| 91久久精品91久久性色| 18岁日韩内射颜射午夜久久成人| 精品一久久香蕉国产线看播放 | 无码人妻精品一区二区三区久久久| 久久大香香蕉国产| 香蕉久久影院| 国产精品一久久香蕉国产线看观看| 久久精品国产精品亜洲毛片| 日韩av无码久久精品免费| 精品人妻伦九区久久AAA片69| 久久婷婷午色综合夜啪| 国产精品日韩欧美久久综合| 影音先锋女人AV鲁色资源网久久| 91久久成人免费| 国产亚洲综合久久系列| 欧美一区二区久久精品| 久久免费国产精品一区二区| 日本人妻丰满熟妇久久久久久| 久久一区二区三区99| 久久国产精品久久国产精品| 亚洲精品高清国产一线久久| 人妻中文久久久久| 91精品婷婷国产综合久久 | 亚洲欧洲久久久精品| 久久综合九色综合精品| 香蕉久久av一区二区三区| 性高湖久久久久久久久AAAAA| 国产亚洲精久久久久久无码AV| 久久久久久国产精品免费无码| 久久精品中文字幕一区| 久久久亚洲裙底偷窥综合| 午夜视频久久久久一区 | 久久久久99精品成人片| 国产91久久综合| 国产福利电影一区二区三区久久久久成人精品综合 | 国产精自产拍久久久久久蜜|