您现在的位置是:群英 > 开发技术 > 编程语言
C++文件查找遍历的方法是什么,怎样实现?
Admin发表于 2021-12-23 19:15:331208 次浏览

    这篇文章我们来了解C++ 文件查找遍历的相关内容,这里使用的是用 _findfirst 和 _findnext 查找文件,那么具体怎样实现呢?下文有很详细的介绍,有需要的朋友可以了解看看,接下来就跟随小编来一起学习一下吧!

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

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 != -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++ 文件查找遍历的方法的介绍,上述示例具有一定的借鉴价值,有需要的朋友可以参考学习,希望对大家学习C++有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。

文本转载自脚本之家

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-04-29 11:56:10 
摘要:给大家带来一篇关于datime包在python中计算时间差的相关教程文章,内容涉及到Python、python教程等相关内容,已被746人关注,更多关于python的内容希望能够帮助到大家。
2022-04-28 17:17:33 
摘要:对于安卓程序员来说,自定义view简直不要太重要,毕竟有很多功能,譬如圆形头像这些,用单纯的原生非常难以实现,而用自定义view,简直分分钟,今天我们来实现自定义圆角输入框和按钮,大家可以跟着练习,掌握技巧
2022-06-16 09:25:23 
摘要:2种方法:1、用array_splice(),只需将第二个参数设为4,第三参数设为1,语法“array_splice($arr,4,1)”。2、用unset(),语法“unset($arr[4])”,适用于索引数组中,删除索引为4的元素即。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部