Chinaunix首页 | 论坛 | 博客
  • 博客访问: 289071
  • 博文数量: 111
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 816
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-04 20:35
文章分类

全部博文(111)

文章存档

2016年(1)

2015年(5)

2014年(105)

我的朋友

分类: C/C++

2014-08-13 23:41:54


点击(此处)折叠或打开

  1. #include "stdlib.h"
  2. #include "direct.h"
  3. #include "string.h"
  4. #include "io.h"
  5. #include "stdio.h"
  6. #include "iostream"
  7. #include <vector>

  8. #include "CBrowseDir.h"

  9. using namespace std;
  10.                                                                                                                    
  11.                                                                                              
  12. CBrowseDir::CBrowseDir()
  13. {
  14.     //用当前目录初始化m_szInitDir
  15.     getcwd(m_szInitDir,_MAX_PATH);
  16.                                                                                              
  17.     //如果目录的最后一个字母不是'\',则在最后加上一个'\'
  18.     int len=strlen(m_szInitDir);
  19.     if (m_szInitDir[len-1] != '\\')
  20.         strcat(m_szInitDir,"\\");
  21.     
  22. }
  23.                                                                                              
  24. bool CBrowseDir::SetInitDir(const char *dir)
  25. {
  26.     //先把dir转换为绝对路径
  27.     if (_fullpath(m_szInitDir,dir,_MAX_PATH) == NULL)
  28.         return false;
  29.                                                                                              
  30.     //判断目录是否存在
  31.     if (_chdir(m_szInitDir) != 0)
  32.         return false;
  33.                                                                                              
  34.     //如果目录的最后一个字母不是'\',则在最后加上一个'\'
  35.     int len=strlen(m_szInitDir);
  36.     if (m_szInitDir[len-1] != '\\')
  37.         strcat(m_szInitDir,"\\");
  38.                                                                                              
  39.     return true;
  40. }
  41.                                                                                              
  42. bool CBrowseDir::BeginBrowse(const char *filespec)
  43. {
  44.     ProcessDir(m_szInitDir,NULL);
  45.     return BrowseDir(m_szInitDir,filespec);
  46. }
  47.                                                                                              
  48. bool CBrowseDir::BrowseDir(const char *dir,const char *filespec)
  49. {
  50.     _chdir(dir);
  51.                                                                                              
  52.     //首先查找dir中符合要求的文件
  53.     long hFile;
  54.     _finddata_t fileinfo;
  55.     if ((hFile=_findfirst(filespec,&fileinfo)) != -1)
  56.     {
  57.         do
  58.         {
  59.             //检查是不是目录
  60.             //如果不是,则进行处理
  61.             if (!(fileinfo.attrib & _A_SUBDIR))
  62.             {
  63.                 char filename[_MAX_PATH];
  64.                 strcpy(filename,dir);
  65.                 strcat(filename,fileinfo.name);
  66.                 cout << filename << endl;
  67.                 if (!ProcessFile(filename))
  68.                     return false;
  69.             }
  70.         } while (_findnext(hFile,&fileinfo) == 0);
  71.         _findclose(hFile);
  72.     }
  73.     //查找dir中的子目录
  74.     //因为在处理dir中的文件时,派生类的ProcessFile有可能改变了
  75.     //当前目录,因此还要重新设置当前目录为dir。
  76.     //执行过_findfirst后,可能系统记录下了相关信息,因此改变目录
  77.     //对_findnext没有影响。
  78.     _chdir(dir);
  79.     if ((hFile=_findfirst("*.*",&fileinfo)) != -1)
  80.     {
  81.         do
  82.         {
  83.             //检查是不是目录
  84.             //如果是,再检查是不是 ...
  85.             //如果不是,进行迭代
  86.             if ((fileinfo.attrib & _A_SUBDIR))
  87.             {
  88.                 if (strcmp(fileinfo.name,".") != 0 && strcmp
  89.                     (fileinfo.name,"..") != 0)
  90.                 {
  91.                     char subdir[_MAX_PATH];
  92.                     strcpy(subdir,dir);
  93.                     strcat(subdir,fileinfo.name);
  94.                     strcat(subdir,"\\");
  95.                     ProcessDir(subdir,dir);
  96.                     if (!BrowseDir(subdir,filespec))
  97.                         return false;
  98.                 }
  99.             }
  100.         } while (_findnext(hFile,&fileinfo) == 0);
  101.         _findclose(hFile);
  102.     }
  103.     return true;
  104. }
  105.                                                                                              
  106. bool CBrowseDir::ProcessFile(const char *filename)
  107. {
  108.     return true;
  109. }
  110.                                                                                              
  111. void CBrowseDir::ProcessDir(const char *currentdir,const char *parentdir)
  112. {
  113. }
  114.                                                                                              
  115. //从CBrowseDir派生出的子类,用来统计目录中的文件及子目录个数
  116. class CStatDir:public CBrowseDir
  117. {
  118. protected:
  119.     int m_nFileCount; //保存文件个数
  120.     int m_nSubdirCount; //保存子目录个数
  121.                                                                                              
  122. public:
  123.     //缺省构造器
  124.     CStatDir()
  125.     {
  126.         //初始化数据成员m_nFileCount和m_nSubdirCount
  127.         m_nFileCount=m_nSubdirCount=0;
  128.     }
  129.                                                                                              
  130.     //返回文件个数
  131.     int GetFileCount()
  132.     {
  133.         return m_nFileCount;
  134.     }
  135.                                                                                              
  136.     //返回子目录个数
  137.     int GetSubdirCount()
  138.     {
  139.         //因为进入初始目录时,也会调用函数ProcessDir,
  140.         //所以减1后才是真正的子目录个数。
  141.         return m_nSubdirCount-1;
  142.     }
  143.                                                                                              
  144. protected:
  145.     //覆写虚函数ProcessFile,每调用一次,文件个数加1
  146.     virtual bool ProcessFile(const char *filename)
  147.     {
  148.         m_nFileCount++;
  149.         return CBrowseDir::ProcessFile(filename);
  150.     }
  151.                                                                                              
  152.     //覆写虚函数ProcessDir,每调用一次,子目录个数加1
  153.     virtual void ProcessDir
  154.         (const char *currentdir,const char *parentdir)
  155.     {
  156.         m_nSubdirCount++;
  157.         CBrowseDir::ProcessDir(currentdir,parentdir);
  158.     }
  159. };

阅读(868) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~