C++用 _findfirst 和 _findnext 查找文件
发布日期:2021-05-14 10:16:55 浏览次数:21 分类:精选文章

本文共 3082 字,大约阅读时间需要 10 分钟。

? _findfirst ? _findnext ????

?????

? C ??? io.h ??????? _finddata_t ????????????????????????????

struct _finddata_t {
unsigned attrib; // ??????
time_t time_create; // ??????
time_t time_access; // ????????
time_t time_write; // ????????
unsigned long _fsize_t size; // ????
char name[260]; // ???
};

????

  • attrib??????????????????

    • _A_ARCH?????
    • _A_HIDDEN?????
    • _A_NORMAL?????
    • _A_RDONLY?????
    • _A_SUBDIR????
    • _A_SYSTEM?????
  • time_create?time_access?time_write?????????????????????????????

  • size??????

  • name?????????? 260 ????

_findfirst ? _findnext ???

1. _findfirst ??

long _findfirst(const char *filename, struct _finddata_t *pfind);
  • ???

    • filename?????????????????? *.* ? *.cpp??
    • pfind????????????????
  • ????

    • ?????????
    • ???? -1?

2. _findnext ??

int _findnext(long handle, struct _finddata_t *pfind);
  • ???

    • handle??????????????
    • pfind??????????????
  • ????

    • ???? 0?
    • ???? -1?

3. _findclose ??

int _findclose(long handle);
  • ????????
  • ????
    • ???? 0?
    • ???? -1?

??????

????? _findfirst ? _findnext ????????????

#include 
#include
#include
using namespace std;
bool transfer(string fileName, int &exeNum) {
_finddata_t fileInfo;
long handle = _findfirst(fileName.c_str(), &fileInfo);
if (handle == -1) {
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;
}
void dfsFolder(string folderPath, ofstream &fout) {
_finddata_t FileInfo;
string strfind = folderPath + "\\*";
long Handle = _findfirst(strfind.c_str(), &FileInfo);
if (Handle == -1) {
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();
}
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;
}

???????

  • ??????????????????? *?*.*?*.exe ???
  • ????? (.) ?????? (..) ??????????????????????????

??????????

?? _findfirst ? _findnext ????????????????????

  • ???????????????????? . ? ..????????
  • ???????????? dfsFolder ?????
  • ???????????????????????
  • ??

    ?????????????????????????? _findfirst ?????????_findnext ??????????_findclose ?????????

    上一篇:Opencv3的svm训练自己图片产生.xml文件模型
    下一篇:批量读取某一文件下所有图片或者其他文件

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年05月03日 03时53分52秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章

    算法的伪码表示 2019-03-11
    递推方程与算法分析 2019-03-11
    主定理的应用 2019-03-11
    动态规划算法的迭代实现 2019-03-11
    最优装载问题 2019-03-11
    最大团问题 2019-03-11
    圆排列问题 2019-03-11
    课程总结 2019-03-11
    认识CMake及应用 2019-03-11
    CMake的主体框架 2019-03-11
    微积分(三) 2019-03-11
    Oracle 2019-03-11
    软件工程应用 2019-03-11
    数据科学 2019-03-11
    论文报告/前沿文章 2019-03-11
    函数与高级变量 2019-03-11
    键盘事件 2019-03-11
    弱监督 2019-03-11
    二 召回算法 2019-03-11
    2020-11月计划实施表 2019-03-11