服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

香港云服务器
服务器之家 - 编程语言 - C/C++ - C++编程使用findfirst和findnext查找及遍历文件实现示例

C++编程使用findfirst和findnext查找及遍历文件实现示例

2022-01-24 14:07ranjiewen C/C++

这篇文章主要为大家介绍了C++编程如何使用findfirst和findnext查找及遍历文件实现示例,有需要的朋友可以借鉴参考下,希望能够有所帮助

这两个函数均在io.h里面

一、首先了解一下一个文件结构体:

?
1
2
3
4
5
6
7
8
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。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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 != -1)
    {
        cout << file.name << endl;
        k = _findnext(HANDLE, &file);
    }
    _findclose(HANDLE);
 
    transfer("C:\\Windows\\*.exe", 0);
    ofstream o_fstream;
 
    dfsFolder("E:\\\WHU\\Study", o_fstream);
    return 0;
}
//_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) == 0);
    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(-1);
    }
    do{
        //判断是否有子目录 
        if (FileInfo.attrib & _A_SUBDIR)
        {
            //这个语句很重要 
            if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
            {
                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) == 0);
 
    _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查找及遍历文件实现示例的详细内容,更多关于findfirst和findnext查找及遍历文件的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/ranjiewen/p/5960976.html

延伸 · 阅读

精彩推荐
  • C/C++C语言实现电脑关机程序

    C语言实现电脑关机程序

    这篇文章主要为大家详细介绍了C语言实现电脑关机程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xiaocaidayong8482021-08-20
  • C/C++学习C++编程的必备软件

    学习C++编程的必备软件

    本文给大家分享的是作者在学习使用C++进行编程的时候所用到的一些常用的软件,这里推荐给大家...

    谢恩铭10102021-05-08
  • C/C++C/C++经典实例之模拟计算器示例代码

    C/C++经典实例之模拟计算器示例代码

    最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示...

    jia150610152021-06-07
  • C/C++C++之重载 重定义与重写用法详解

    C++之重载 重定义与重写用法详解

    这篇文章主要介绍了C++之重载 重定义与重写用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    青山的青6062022-01-04
  • C/C++C语言中炫酷的文件操作实例详解

    C语言中炫酷的文件操作实例详解

    内存中的数据都是暂时的,当程序结束时,它们都将丢失,为了永久性的保存大量的数据,C语言提供了对文件的操作,这篇文章主要给大家介绍了关于C语言中文件...

    针眼_6702022-01-24
  • C/C++深入理解goto语句的替代实现方式分析

    深入理解goto语句的替代实现方式分析

    本篇文章是对goto语句的替代实现方式进行了详细的分析介绍,需要的朋友参考下...

    C语言教程网7342020-12-03
  • C/C++详解c语言中的 strcpy和strncpy字符串函数使用

    详解c语言中的 strcpy和strncpy字符串函数使用

    strcpy 和strcnpy函数是字符串复制函数。接下来通过本文给大家介绍c语言中的strcpy和strncpy字符串函数使用,感兴趣的朋友跟随小编要求看看吧...

    spring-go5642021-07-02
  • C/C++c++ 单线程实现同时监听多个端口

    c++ 单线程实现同时监听多个端口

    这篇文章主要介绍了c++ 单线程实现同时监听多个端口的方法,帮助大家更好的理解和学习使用c++,感兴趣的朋友可以了解下...

    源之缘11542021-10-27
1132