C++利用 _findfirst與_findnext查找文件的方法
C++ 文件查找
在C++中我們要如何查找文件呢?我們需要一個(gè)結(jié)構(gòu)體和幾個(gè)大家可能不太熟悉的函數(shù)。這些函數(shù)和結(jié)構(gòu)體在的頭文件中,結(jié)構(gòu)體為struct _finddata_t ,函數(shù)為_findfirst、_findnext和_fineclose。具體如何使用,下面來一起看看吧
_findfirst與_findnext查找文件
一、這兩個(gè)函數(shù)均在io.h里面。
二、首先了解一下一個(gè)文件結(jié)構(gòu)體:
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,其實(shí)就是long
而_fsize_t,就是unsigned long
現(xiàn)在來解釋一下結(jié)構(gòu)體的數(shù)據(jù)成員吧。
attrib,就是所查找文件的屬性:_A_ARCH(存檔)、_A_HIDDEN(隱藏)、_A_NORMAL(正常)、_A_RDONLY(只讀)、 _A_SUBDIR(文件夾)、_A_SYSTEM(系統(tǒng))。
time_create、time_access和time_write分別是創(chuàng)建文件的時(shí)間、最后一次訪問文件的時(shí)間和文件最后被修改的時(shí)間。
size:文件大小
name:文件名。
三、用 _findfirst 和 _findnext 查找文件
1、_findfirst函數(shù):long _findfirst(const char *, struct _finddata_t *);
第一個(gè)參數(shù)為文件名,可以用"*.*"來查找所有文件,也可以用"*.cpp"來查找.cpp文件。第二個(gè)參數(shù)是_finddata_t結(jié)構(gòu)體指針。若查找成功,返回文件句柄,若失敗,返回-1。
2、_findnext函數(shù):int _findnext(long, struct _finddata_t *);
第一個(gè)參數(shù)為文件句柄,第二個(gè)參數(shù)同樣為_finddata_t結(jié)構(gòu)體指針。若查找成功,返回0,失敗返回-1。
3、_findclose()函數(shù):int _findclose(long);
只有一個(gè)參數(shù),文件句柄。若關(guān)閉成功返回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 函數(shù)返回的是匹配到文件的句柄,數(shù)據(jù)類型為long。
//遍歷過程可以指定文件類型,這通過FileName的賦值來實(shí)現(xiàn),例如要遍歷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;
}
//遍歷文件夾及其子文件夾下所有文件。操作系統(tǒng)中文件夾目錄是樹狀結(jié)構(gòu),使用深度搜索策略遍歷所有文件。用到_A_SUBDIR屬性
//在判斷有無子目錄的if分支中,由于系統(tǒng)在進(jìn)入一個(gè)子目錄時(shí),匹配到的頭兩個(gè)文件(夾)是"."(當(dāng)前目錄),".."(上一層目錄)。
//需要忽略掉這兩種情況。當(dāng)需要對遍歷到的文件做處理時(shí),在else分支中添加相應(yīng)的代碼就好
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)
{
//這個(gè)語句很重要
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 << " 系統(tǒng)文件 ";
// }
// else if (file.attrib == _A_SUBDIR)
// {
// cout << " 子目錄 ";
// }
// else
// {
// cout << " 存檔文件 ";
// }
// cout << endl;
// } while (_findnext(longf, &file) == 0);//int __cdecl _findnext(long, struct _finddata_t *);如果找到下個(gè)文件的名字成功的話就返回0,否則返回-1
//
// _findclose(longf);
//
// return 0;
//}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。
上一篇:C++判斷主機(jī)是否處于聯(lián)網(wǎng)狀態(tài)
欄 目:C語言
下一篇:對C++ string append方法的常用用法詳解
本文標(biāo)題:C++利用 _findfirst與_findnext查找文件的方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/759.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)


