您的位置:首页 > 其它

用 _findfirst 和 _findnext 查找文件

2016-05-11 20:24 309 查看
一、这两个函数均在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>

using namespace std;

int main()

{

_finddata_t
file;

int
k;

long
HANDLE;

k
= HANDLE = _findfirst( "*.*", &file );

while(
k != -1 )

{

cout
<< file.name << endl;

k
= _findnext( HANDLE, &file );

}

_findclose(
HANDLE );

return
0;

}

转自:http://blog.sina.com.cn/s/blog_67e046d10100jwdo.html

这几天在弄一个项目,需要读取给定路径下的所有文件夹名称或所有带后缀的文件名。

  查了下C++的资料,发现有很多方法,这里总结其中的一种,其他用法类似。

  主要用到了以下几个头文件(类):io.h, fstream, string。

  1、读取某给定路径下所有文件夹与文件名称,并带完整路径。代码如下:

1 void getAllFiles( string path, vector<string>& files)
2 {
3     //文件句柄
4     long   hFile   =   0;
5     //文件信息
6     struct _finddata_t fileinfo;  //很少用的文件信息读取结构
7     string p;  //string类很有意思的一个赋值函数:assign(),有很多重载版本
8     if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
9     {
10         do
11         {
12             if((fileinfo.attrib &  _A_SUBDIR))  //比较文件类型是否是文件夹
13             {
14                 if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)
15                 {
16                     files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
17                     getFilesall( p.assign(path).append("\\").append(fileinfo.name), files );
18                 }
19             }
20             else
21             {
22                 files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
23             }
24         }while(_findnext(hFile, &fileinfo)  == 0);  //寻找下一个,成功返回0,否则-1
25         _findclose(hFile);
26     }
27 }


  该函数有两个参数,第一个为路径字符串(string类型,最好为绝对路径);第二个参数为文件夹与文件名称存储变量(vector类型,引用传递)。在主函数中调用格式(并将结果保存在文件"AllFiles.txt"中,第一行为总数):

1 char * filePath = "E:\\YunShi";
2 vector<string> files;
3 char * distAll = "AllFiles.txt";
4 getFilesall(filePath, files);
5 ofstream ofn(distAll);
6 int size = files.size();
7 ofn<<size<<endl;
8 for (int i = 0;i<size;i++)
9 {
10     ofn<<files[i]<<endl;
11 }
12 ofn.close();


  结果如下:



  同理,只读取某给定路径下的当前文件夹名(以下类似,只给出函数,调用案例同上):

1 void getJustCurrentDir( string path, vector<string>& files)
2 {
3     //文件句柄
4     long   hFile   =   0;
5     //文件信息
6     struct _finddata_t fileinfo;
7     string p;
8     if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
9     {
10         do
11         {
12             if((fileinfo.attrib &  _A_SUBDIR))
13             {
14                 if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)
15                 {
16                     files.push_back(fileinfo.name);
17                     //files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
18                 }
19
20             }
21         }while(_findnext(hFile, &fileinfo)  == 0);
22         _findclose(hFile);
23     }
24 }


  只读取某给定路径下的当前文件名:

1 void getJustCurrentFile( string path, vector<string>& files)
2 {
3     //文件句柄
4     long   hFile   =   0;
5     //文件信息
6     struct _finddata_t fileinfo;
7     string p;
8     if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
9     {
10         do
11         {
12             if((fileinfo.attrib &  _A_SUBDIR))
13             {
14                 ;
15             }
16             else
17             {
18                 files.push_back(fileinfo.name);
19                 //files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
20             }
21         }while(_findnext(hFile, &fileinfo)  == 0);
22         _findclose(hFile);
23     }
24 }


  只读取某给定路径下的所有文件名(即包含当前目录及子目录的文件):

1 void getFilesAll( string path, vector<string>& files)
2 {
3     //文件句柄
4     long   hFile   =   0;
5     //文件信息
6     struct _finddata_t fileinfo;
7     string p;
8     if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
9     {
10         do
11         {
12             if((fileinfo.attrib &  _A_SUBDIR))
13             {
14                 if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)
15                 {
16                     //files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
17                     getFilesA( p.assign(path).append("\\").append(fileinfo.name), files );
18                 }
19             }
20             else
21             {
22                 files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
23             }
24         }while(_findnext(hFile, &fileinfo)  == 0);
25         _findclose(hFile);
26     }
27 }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: