Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8465
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-27 22:31
个人简介

风过竹林~那种感觉很神奇

文章分类

全部博文(7)

文章存档

2015年(3)

2014年(4)

我的朋友
最近访客

分类: C/C++

2015-01-05 09:52:52

// VC下遍历文件夹下所有文件或者特定格式的文件,vs2008下运行OK


点击(此处)折叠或打开

  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <string>
  6. #include <map>
  7. #include <utility>

  8. using namespace std;

  9. void fileFind(char* lpPath, map<string, int>& mapFile)
  10. {
  11.     int count = 0;
  12.     
  13.     char curFind[MAX_PATH];//定义一个char数组存放路径,MAX_PATH为路径最大长度,为260

  14.     strcpy(curFind,lpPath); // 将路径copy到当前的数组
  15.     strcat(curFind,"*.*"); //在数组后加上*.*,匹配文件和文件夹


  16.     WIN32_FIND_DATA fileData; //定义文件的九种属性
  17.     HANDLE fileHandle = ::FindFirstFile(curFind, &fileData); //获得文件句柄
  18.     //cout<<fileData.cFileName<<endl;
  19.     while(::FindNextFile(fileHandle, &fileData)) //循环遍历查找文件和文件夹
  20.     {
  21.         if (fileData.cFileName[0] == '.') //当前目录或者上一目录,cFileName是文件名,cFileName[0]为第一个字符
  22.         {
  23.             continue;
  24.         }
  25.         if (fileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ) //如果是目录
  26.         {            
  27.             //cout<<"!!!!!!!!!!!!!"<<endl; //分割标示
  28.             //cout<<fileData.cFileName<<"***"<<endl;
  29.             //mapFile.insert(make_pair(fileData.cFileName, count));  // 把目录存入map
  30.             strcpy(curFind,lpPath);
  31.             strcat(curFind,fileData.cFileName);
  32.             strcat(curFind, "\\"); // 给文件夹加上\字符
  33.             fileFind(curFind, mapFile);         // 继续递归查找

  34.             cout<<"!!!!!!!!!!!!!"<<endl;
  35.         }
  36.         //mapFile.insert(make_pair(fileData.cFileName, count));   //不判断则将全部文件存入map
  37.         int size= 0;
  38.         while (fileData.cFileName[size] != '\0' )
  39.         {
  40.             size ++;
  41.         }
  42.         if (fileData.cFileName[size-4] == '.' && fileData.cFileName[size-3]=='t'
  43.             && fileData.cFileName[size-2]=='x' && fileData.cFileName[size-1]=='t')   //判断是不是 .txt, 若
  44.         {
  45.             mapFile.insert(make_pair(fileData.cFileName, count));   // 把txt文件存入map
  46.             //cout<<fileData.cFileName<<endl;    
  47.         }

  48.     }
  49. }
  50. int _tmain(int argc, _TCHAR* argv[])
  51. {
  52.     map<string ,int> mapFile;

  53.     char filePath[] = "E:\\x\\";
  54.     fileFind(filePath,mapFile);
  55.     map<string, int>::iterator mapFileFind = mapFile.find("87686.txt");
  56.     if (mapFileFind != mapFile.end())
  57.     {
  58.         cout<< "It is right%%%%%%%%%"<<endl;
  59.     }
  60.     else
  61.         cout<<"It id false @@@@@@@@@"<<endl;
  62.     map<string, int>::iterator it = mapFile.begin();
  63.     for (; it != mapFile.end(); ++it)
  64.     {
  65.         cout<<it->first<<endl;
  66.     }
  67.     return 0;
  68. }
运行如下:

!!!!!!!!!!!!!

!!!!!!!!!!!!!

!!!!!!!!!!!!!


!!!!!!!!!!!!!

!!!!!!!!!!!!!

It is right%%%%%%%%%
87686.txt
erwerwesfsdf.txt
fsgfg.txt
rtert.txt
sdfs.txt
xxx.txt
复件 (2) 87686.txt
复件 (2) erwerwesfsdf.txt
复件 (2) fsgfg.txt
复件 (2) rtert.txt
复件 (2) sdfs.txt
复件 (2) xxx.txt
复件 (3) 87686.txt
复件 (3) erwerwesfsdf.txt

如果运行不了,或者提示错误:错误 3 error C2664: “FindFirstFileW”: 不能将参数 1 从“char [260]”转换为“LPCWSTR” c:\documents and settings\zhangyongzhao\my documents\visual studio 2008\projects\finaltraservalfile\finaltraservalfile\finaltraservalfile.cpp 23


请将工程->属性->配置属性->常规->项目默认值->字符集 修改为“未设置”或“多字符字节”!

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