Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1756346
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: C/C++

2012-06-12 08:40:12

 

点击(此处)折叠或打开

  1. /* 文件名:searchAllFile.cpp
  2.  * int searchAllFile(string filePath, //要搜索的文件路径
  3.  * int layer //layer==0 //搜索的层次,输入路径的层次应该为0
  4.  * string fileNameToSave); //存储文件信息的文件名,包括路径
  5.  *
  6.  *
  7.  *
  8.  *
  9.  *
  10.  *
  11.  * <io.h>中定义了结构体 struct _finddata64i32_t (C风格),用来存储文件的各种信息
  12.  *详细如下:
  13.  *struct _finddata64i32_t
  14.  * {
  15.  * unsigned attrib;
  16.  * __time64_t time_create; // -1 for FAT file systems
  17.  * __time64_t time_access; // -1 for FAT file systems
  18.  * __time64_t time_write;
  19.  * _fsize_t size;
  20.  * char name[260];
  21.  * };
  22.  *各参数意义如下:
  23.  *unsigned attrib :4个字节,存储文件的属性
  24.  * _A_ARCH (存档) 0x20
  25.  * _A_SUBDIR(文件夹)0x10
  26.  * _A_SYSTEM(系统)0x04
  27.  * _A_HIDDEN(隐藏)0x02
  28.  * _A_RDONLY(只读)0x01
  29.  * _A_NORMAL(正常)0X00
  30.  *这些都是<io.h>中定义的宏,每一个都是一个unsigned int,各属性叠加时进行或运算,如_A_HIDDEN|_A_RDONLY
  31.  *
  32.  *__time64_t time_create: 文件创建的时间
  33.  *__time64_t time_access: 文件最后一次访问的时间
  34.  *__time64_t time_write: 文件最后以此修改的时间
  35.  *_fsize_t size: 文件的大小,字节为单位
  36.  *char name[260]: 文件名
  37.  *
  38.  *--------------------------------------------------------------------------------------------------------------------------------
  39.  *<io.h> 中定义了两个函数
  40.  *long _findfirst64i32(const char * _Filename,struct _finddata64i32_t * _FindData); ,查找第一个_Filename的信息,存储到结构体_FindData中             
  41.  * 查找成功,返回一个用于继续查找的句柄(一个唯一的编号)
  42.  * 查找 失败,返回-1
  43.  *int _findnext64i32(long handle,struct _finddata64i32_t *fileinfo) ; 根据句柄handle查找下一个文件,存放在 fileinfo中
  44.  * 查找成功,返回0,失败返回-1
  45.  *ing _findclose(long handle); 关闭句柄handle,成功返回0,失败返回-1
  46.  *---------------------------------------------------------------------------------------------------------------------------------------
  47.  */
  48. #include<iostream>
  49. #include<string>
  50. #include<fstream>
  51. #include<io.h> //定义了结构体struct _finddata64i32_t(该结构体包含文件的相关属性,如文件名,文件的属性等
  52.     //定义函数: long _findfirst64i32(char* fileName,struct _finddata64i32_t *fileinf0);
  53.     //定义函数: int _findnext64i32(long handle,struct _finddata64i32_t *fileinfo);
  54.     //定义函数: int _findclose(long handle);
  55. using namespace std;
  56. //定义链表结点
  57. struct fileInfoNode
  58. {
  59.  struct _finddata64i32_t fileInfo; //保存文件信息的结构体
  60.  string fileName;
  61.  struct fileInfoNode* left;
  62. };
  63. //把文件信息连接到链表head中
  64. int saveToLink(struct fileInfoNode*& head, //链表的头结点,引用参量
  65.  const string& fileName, //IN:文件名(包括路径)
  66.  const struct _finddata64i32_t& fileInfo) //IN:文件信息结构体,引用参量
  67. {
  68.  //建立一个结点
  69.   fileInfoNode* p;
  70.   p=new fileInfoNode;
  71.   p->fileInfo=fileInfo; //把传入的文件信息复制进结点
  72.   p->fileName=fileName;
  73.   p->left=head;
  74.   head=p;
  75.   return 0;
  76. }
  77. //显示整个查找到的文件的信息
  78. void displayLink(struct fileInfoNode* head)//IN:头结点,值传递参数
  79. {
  80.  while(head!=NULL)
  81.  {
  82.   cout<<"fileName: "<<head->fileName<<endl;
  83.   cout<<"fileSize: "<<dec<<head->fileInfo.size<<"字节"<<endl;
  84.   cout<<"fileAttrib: "<<"0x"<<hex<<head->fileInfo.attrib<<endl;
  85.   cout<<"-------------------------------------------------------------------------------------------"<<endl;
  86.   head=head->left;
  87.  }
  88. }
  89. //把文件信息存储到文件 fileName 中
  90. void saveLinkToFile(struct fileInfoNode* head,string saveFileName,int counter)
  91. {
  92.  ofstream fout;
  93.  //打开文件
  94.  fout.open(saveFileName.c_str());
  95.  if((fout.is_open())==false)
  96.  {
  97.   cout<<"存储文件打开失败!"<<endl;
  98.   exit(-1);
  99.  }
  100.  fout<<"the file number is: "<<counter<<endl;
  101.  fout<<"-------------------------------------------------------------------------------------------------------"<<endl;
  102.  while(head!=NULL)
  103.  {
  104.   fout<<"fileName: "<<head->fileName<<endl;
  105.   fout<<"fileSize: "<<dec<<head->fileInfo.size<<"字节"<<endl;
  106.   fout<<"fileAttrib: "<<"0x"<<hex<<head->fileInfo.attrib<<endl;
  107.   fout<<"-------------------------------------------------------------------------------------------------------"<<endl;
  108.   head=head->left;
  109.  }
  110.  //关闭文件
  111.  fout.close();
  112. }
  113. //
  114. int searchAllFile(string filePath,//IN:文件所在的路径,如:f:\example
  115.  int layer,//层次,只有层次为0时,才完成链表中文件信息的显示和存储
  116.    string fileInfoOut) //IN:存储的文件名
  117. {
  118.  struct _finddata64i32_t fileInfo;//保存文件信息的结构体
  119.  static fileInfoNode* head=NULL; //fileInfoNode链表的头结点,静态存储
  120.  static int counter=0; //记录文件数目
  121.  long handle;//句柄
  122.  int done;//查找nextfile是否成功
  123.  string fileName=filePath+"\\*.*"; //要搜索的文件名
  124.  //查找第一个文件,返回句柄
  125.  handle=_findfirst64i32(fileName.c_str(),&fileInfo);
  126.  if(handle==-1)
  127.  {
  128.   cout<<"该目录为空!"<<endl;
  129.   //cin.get();
  130.    return -1;
  131.  }
  132.  do
  133.  {
  134.  // cout<<"查找成功"<<endl;
  135.  // cin.get();
  136.  // cout<<fileInfo.name<<endl;
  137.   //如果是文件夹".",或者"..",则进行判断下一个文件
  138.   if((strcmp(fileInfo.name,".")==0)|(strcmp(fileInfo.name,"..")==0))
  139.   {
  140.    //cout<<"丢弃!"<<endl;
  141.    //cin.get();
  142.    continue;
  143.   }
  144.   //如果是文件夹,则进入下一层文件夹搜索
  145.   if((fileInfo.attrib&_A_SUBDIR)==_A_SUBDIR)
  146.   {
  147.  // cout<<"是文件夹"<<endl;
  148.  // cin.get();
  149.    string filePathSub=filePath+"\\"+fileInfo.name;
  150.    //递归调用
  151.    searchAllFile(filePathSub,++layer,fileInfoOut);
  152.    layer--;
  153.   }
  154.   //把搜集到的信息连接到文件
  155.   else
  156.   {
  157. // cout<<"是文件,存储信息!"<<endl;
  158. // cin.get();
  159.    counter++;
  160.    string fileNameTure=filePath+"\\"+fileInfo.name;
  161.    saveToLink(head,fileNameTure,fileInfo); //存储到链表中
  162.   }
  163.  }while(!(done=_findnext64i32(handle,&fileInfo)));
  164.  _findclose(handle);
  165.  //layer==时,完成链表的存储
  166.  if(layer==0)
  167.  {
  168.   //显示链表中的内容
  169.    displayLink(head);
  170.   //存储链表中的内容
  171.    saveLinkToFile(head,fileInfoOut,counter);
  172.  }
  173.  return 0;
  174. }


 

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