一、这两个函数均在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. Python 数据结构--查找

    1 顺序查找O(n) def sequential_search(a_list, item): pos = 0 found = False while pos < len(a_list) and ...

  2. JAVA_关键词01_instanceof的应用

    A instanceof  B: 对象A是否是 B类的一个实例 应用举例:

  3. POJ 2234 Matches Game(Nim博弈裸题)

    Description Here is a simple game. In this game, there are several piles of matches and two players. ...

  4. table中head表头固定,body滚动

    <style type="text/css"> .table-head { background-color: #; color: #; } .table-body { ...

  5. Github笔记(1)

    学习目的: 借助GitHub托管项目代码 GitHub官方介绍: 中文:http://www.cnblogs.com/twtp/articles/5264073.html 英文:https://gui ...

  6. 论Injection的前世今生

    Click me~ why Java EE provides injection mechanisms that enable your objects to obtain references to ...

  7. 1100C NN and the Optical Illusion

    推公式,水题.cos()函数是默认弧度制的 #include <iostream> #include <cstring> #include <string> #in ...

  8. jquery选择后代以及toggle,toggleClass用法

    $("#id").find(".classname")      选择id为xx下的类名为xx的元素 toggle  $(this).next(".c ...

  9. 网页筛选Automatic Input Enrichment - FLAIR

    http://www.cs.rochester.edu/~tetreaul/Presentations-and-Posters/0504.pdf 介绍了一个在线平台https://eflnotes.w ...

  10. lecture4特征提取-七月在线-cv

    霍夫变换 http://blog.csdn.net/sudohello/article/details/51335237 http://blog.csdn.net/glouds/article/det ...