一、这两个函数均在io.h里面。
 
二、首先了解一下一个文件结构体:
struct _finddata_t {
    unsigned    attrib;
    time_t      time_create;   
    time_t      time_access;   
    time_t      time_write;
    _fsize_t    size;
    char        name[260];
};
 
time_t,其实就是long
而_fsize_t,就是unsigned long
 
现在来解释一下结构体的数据成员吧。
 
attrib,就是所查找文件的属性:_A_ARCH(存档)、_A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、 _A_SUBDIR(文件夹)、_A_SYSTEM(系统)。
 
time_create、time_access和time_write分别是创建文件的时间、最后一次访问文件的时间和文件最后被修改的时间。
 
size:文件大小
 
name:文件名。
 
 
三、用 _findfirst 和 _findnext 查找文件
 
1、_findfirst函数:long _findfirst(const char *, struct _finddata_t *);
 
第一个参数为文件名,可以用"*.*"来查找所有文件,也可以用"*.cpp"来查找.cpp文件。第二个参数是_finddata_t结构体指针。若查找成功,返回文件句柄,若失败,返回-1。
 
 
2、_findnext函数:int _findnext(long, struct _finddata_t *);
 
第一个参数为文件句柄,第二个参数同样为_finddata_t结构体指针。若查找成功,返回0,失败返回-1。
 
3、_findclose()函数:int _findclose(long);
 
只有一个参数,文件句柄。若关闭成功返回0,失败返回-1。
 
#include <io.h>
#include <iostream>
#include <fstream>
using namespace std; bool transfer(string fileName, int exeNum );
void dfsFolder(string folderPath, ofstream &fout); int main()
{
_finddata_t file;
int k;
long HANDLE;
k = HANDLE = _findfirst("*.*", &file);
while (k != -)
{
cout << file.name << endl;
k = _findnext(HANDLE, &file);
}
_findclose(HANDLE); transfer("C:\\Windows\\*.exe", );
ofstream o_fstream; dfsFolder("E:\\\WHU\\Study", o_fstream); return ;
} //_findfirst 函数返回的是匹配到文件的句柄,数据类型为long。
//遍历过程可以指定文件类型,这通过FileName的赋值来实现,例如要遍历C : \WINDOWS下的所有.exe文件 bool transfer(string fileName , int exeNum)
{
_finddata_t fileInfo;
long handle = _findfirst(fileName.c_str(), &fileInfo); if (handle == -1L)
{
cerr << "failed to transfer files" << endl;
return false;
} do
{
exeNum++;
cout << fileInfo.name << endl;
} while (_findnext(handle, &fileInfo) == );
cout << " .exe files' number: " << exeNum << endl; return true;
} //遍历文件夹及其子文件夹下所有文件。操作系统中文件夹目录是树状结构,使用深度搜索策略遍历所有文件。用到_A_SUBDIR属性 //在判断有无子目录的if分支中,由于系统在进入一个子目录时,匹配到的头两个文件(夹)是"."(当前目录),".."(上一层目录)。
//需要忽略掉这两种情况。当需要对遍历到的文件做处理时,在else分支中添加相应的代码就好 void dfsFolder(string folderPath, ofstream &fout)
{
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo); if (Handle == -1L)
{
cerr << "can not match the folder path" << endl;
exit(-);
}
do{
//判断是否有子目录
if (FileInfo.attrib & _A_SUBDIR)
{
//这个语句很重要
if ((strcmp(FileInfo.name, ".") != ) && (strcmp(FileInfo.name, "..") != ))
{
string newPath = folderPath + "\\" + FileInfo.name;
dfsFolder(newPath, fout);
}
}
else
{
fout<<folderPath.c_str() << "\\" << FileInfo.name << " ";
cout << folderPath.c_str() << "\\" << FileInfo.name << endl;
}
} while (_findnext(Handle, &FileInfo) == ); _findclose(Handle);
fout.close();
} //#include <iostream>
//#include <string>
//#include <io.h>
//using namespace std;
//
//int main()
//{
// _finddata_t file;
// long longf;
// string tempName;
// //_findfirst返回的是long型; long __cdecl _findfirst(const char *, struct _finddata_t *)
// if ((longf = _findfirst("E:\\WHU\\Study\\*.*", &file)) == -1l)
// {
// cout << "文件没有找到!\n";
// return 0;
// }
// do
// {
// cout << "文件列表:\n";
// tempName = file.name;
// if (tempName[0] == '.')
// continue;
// cout << file.name<<endl;
//
// if (file.attrib == _A_NORMAL)
// {
// cout << " 普通文件 ";
// }
// else if (file.attrib == _A_RDONLY)
// {
// cout << " 只读文件 ";
// }
// else if (file.attrib == _A_HIDDEN)
// {
// cout << " 隐藏文件 ";
// }
// else if (file.attrib == _A_SYSTEM)
// {
// cout << " 系统文件 ";
// }
// else if (file.attrib == _A_SUBDIR)
// {
// cout << " 子目录 ";
// }
// else
// {
// cout << " 存档文件 ";
// }
// cout << endl;
// } while (_findnext(longf, &file) == 0);//int __cdecl _findnext(long, struct _finddata_t *);如果找到下个文件的名字成功的话就返回0,否则返回-1
//
// _findclose(longf);
//
// return 0;
//}

C++用 _findfirst 和 _findnext 查找文件的更多相关文章

  1. locate 最快的查找文件的命令 NB

    我见过最NB的查找文件最快的命令 [root@NB data]# locate teamviewer. /data/Software/teamviewer.i686.rpm /home/ok/.loc ...

  2. Linux如何搜索查找文件里面内容

    在Linux系统当中,如何搜.索查找文件里面的内容呢? 这个应该是系统维护.管理当中遇到最常见的需求.那么下面介绍,总结一下如何搜索.查找文件当中的内容. 搜索.查找文件当中的内容,一般最常用的是gr ...

  3. Linux下查找文件:which、whereis、locate、find 命令的区别

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.which       查看可执行文件的位置,通过环境变量查whereis    查看文件的位置,通过数据库查,每 ...

  4. linux 根据文件大小查找文件

    inux下的find命令用来查找文件,通过man find就知道它是无所不能的.所以按照文件大小来查找文件就不在话下.从man find搜索size,可以看到如下信息: -size n[cwbkMG] ...

  5. Linux 利用 locate 和 find 查找文件

    Linux 利用 locate 和 find 查找文件 命令 locate 用于快速查找文件.文件夹.此命令并没有在磁盘上查找所有文件,而是在预先建立的数据库里进行搜索.可以使用 updatedb 命 ...

  6. Linux 查找文件

    find 查找目录 -name "文件名"find / -name "php.ini"locate 文件名locate php.ini 一:locate命令 l ...

  7. 53-whereis 查找文件

    查找文件 whereis [options] file 参数 file 是whereis需要查找的文件,这些文件属于原始代码,二进制文件或是帮助文件 选项 -b               只查找二进 ...

  8. 14-find 查找文件

    find - search for files in a directory hierarchy 查找文件 [语法]: find [选项] [参数] [功能介绍] find命令用来在指定目录下查找文件 ...

  9. Linux里如何查找文件内容

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...

随机推荐

  1. Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置

    Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...

  2. maven导出项目依赖的jar包

    摘要: 在进行项目部署时,需要将maven项目所依赖的jar导出到指定目录,本文讲解如何导出项目依赖的jar包 一.导出到默认目录 targed/dependency 从Maven项目中导出项目依赖的 ...

  3. MySQL篇,第三章:数据库知识3

    MySQL 数据库 3 索引 1.普通索引(MUL)   2.唯一索引(UNI)   3.主键索引(PRI) 1.使用规则 1.一个表中只能有一个主键(primary)字段 2.对应字段的值不允许重复 ...

  4. AE1

    i2.1 导入四种方式: 1.文件->导入 2.项目窗口空白区域双击 3.空白区域右键 4 ctrl+i 常用键: ctri+i    ctrl shift  alt delete 2.2移动属 ...

  5. PTA——输出各位数字

    PTA 7-37 输出整数各位数字 方法1: #include <stdio.h> #define N 10000 int main(){ long n, temp; ; scanf(&q ...

  6. 关于vs设置其他主题配色问题

    可以再网上找其他的.vssettings文件导入 导入方法如下: 一般vs会将你之前设置下的配色方案自动保存下来,你也可以直接覆盖 2019-03-21  17:31:07

  7. ELFhash

    字符串哈希算法(以ELFHash详解)   更多字符串哈希算法请参考:http://blog.csdn.net/AlburtHoffman/article/details/19641123 先来了解一 ...

  8. hdu1695 GCD 容斥原理

    Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) ...

  9. 深入学习Motan系列(一)——入门及知识zookeeper储备

    背景以及说明: 最近逮到个RPC框架,打算深入学习,框架千千万,只有懂得内部原理,才能应对复杂的业务,进行自定义化系统. 这个系列的Motan文章也是自己慢慢摸索的轨迹,将这个过程记录下来,一是提升自 ...

  10. 【传输协议】什么是CA证书

    1.什么是CA证书. 看过一些博客,写的比较形象具体. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 B 公司的所有人都不认识他,他咋办捏?常 ...