国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++利用 _findfirst與_findnext查找文件的方法

2020-01-26 13:41:38
字體:
來源:轉載
供稿:網友

C++ 文件查找

在C++中我們要如何查找文件呢?我們需要一個結構體和幾個大家可能不太熟悉的函數。這些函數和結構體在的頭文件中,結構體為struct _finddata_t ,函數為_findfirst、_findnext和_fineclose。具體如何使用,下面來一起看看吧

_findfirst與_findnext查找文件

一、這兩個函數均在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 != -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 + "http://*";  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 + "http://" + FileInfo.name;        dfsFolder(newPath, fout);      }    }    else    {      fout<<folderPath.c_str() << "http://" << FileInfo.name << " ";      cout << folderPath.c_str() << "http://" << 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;//}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博客| 通河县| 阳朔县| 衡南县| 海晏县| 天峨县| 西畴县| 交城县| 盐边县| 安宁市| 金塔县| 南通市| 新晃| 普安县| 米泉市| 黑龙江省| 北京市| 桂平市| 商丘市| 社会| 宿松县| 饶阳县| 商河县| 道孚县| 南康市| 临沭县| 泸州市| 罗平县| 象州县| 桑植县| 华容县| 沭阳县| 清水河县| 兴义市| 财经| 浏阳市| 拜泉县| 长丰县| 远安县| 宝鸡市| 阜新市|