生活随笔
收集整理的這篇文章主要介紹了
C++ 从文件夹中读取文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
OpenCV從文件夾中讀取內含文件方法
參考:http://www.2cto.com/kf/201407/316515.html
http://www.it610.com/article/5126146.htm
http://blog.csdn.net/adong76/article/details/39432467
windows平臺代碼:
[cpp]?view plaincopy
#include?<io.h>?? #include?<fstream>?? #include?<string>?? #include?<vector>?? #include?<iostream>?? ?using?namespace?std;?? ?? ?? ?? void?GetAllFiles(?string?path,?vector<string>&?files)???? {???? ?? ????long???hFile???=???0;???? ?????? ????struct?_finddata_t?fileinfo;???? ????string?p;???? ????if((hFile?=?_findfirst(p.assign(path).append("\\*").c_str(),&fileinfo))?!=??-1)???? ????{???? ????????do???? ????????{????? ????????????if((fileinfo.attrib?&??_A_SUBDIR))???? ????????????{???? ????????????????if(strcmp(fileinfo.name,".")?!=?0??&&??strcmp(fileinfo.name,"..")?!=?0)???? ????????????????{?? ????????????????????files.push_back(p.assign(path).append("\\").append(fileinfo.name)?);?? ????????????????????GetAllFiles(?p.assign(path).append("\\").append(fileinfo.name),?files?);??? ????????????????}?? ????????????}???? ????????????else???? ????????????{???? ????????????????files.push_back(p.assign(path).append("\\").append(fileinfo.name)?);???? ????????????}??? ?? ????????}while(_findnext(hFile,?&fileinfo)??==?0);???? ?? ????????_findclose(hFile);??? ????}??? ?? }???? ?? ?? void?GetAllFormatFiles(?string?path,?vector<string>&?files,string?format)???? {???? ?????? ????long???hFile???=???0;???? ?????? ????struct?_finddata_t?fileinfo;???? ????string?p;???? ????if((hFile?=?_findfirst(p.assign(path).append("\\*"?+?format).c_str(),&fileinfo))?!=??-1)???? ????{???? ????????do???? ????????{?????? ????????????if((fileinfo.attrib?&??_A_SUBDIR))???? ????????????{???? ????????????????if(strcmp(fileinfo.name,".")?!=?0??&&??strcmp(fileinfo.name,"..")?!=?0)???? ????????????????{?? ?????????????????????? ????????????????????GetAllFormatFiles(?p.assign(path).append("\\").append(fileinfo.name),?files,format);??? ????????????????}?? ????????????}???? ????????????else???? ????????????{???? ????????????????files.push_back(p.assign(path).append("\\").append(fileinfo.name)?);???? ????????????}???? ????????}while(_findnext(hFile,?&fileinfo)??==?0);???? ?? ????????_findclose(hFile);??? ????}??? }??? ?? ?? ?? ?? ?? int?main()?? {?? ????string?filePath?=?"testimages\\water";???? ????vector<string>?files;???? ????char?*?distAll?=?"AllFiles.txt";?? ?? ?????? ?????? ?? ?????? ????string?format?=?".jpg";?? ????GetAllFormatFiles(filePath,?files,format);?? ????ofstream?ofn(distAll);?? ????int?size?=?files.size();??? ????ofn<<size<<endl;?? ????for?(int?i?=?0;i<size;i++)???? ????{???? ????????ofn<<files[i]<<endl;??? ????????cout<<?files[i]?<<?endl;?? ????}?? ????ofn.close();?? ????return?0;?? }??
Linux平臺代碼:
[cpp]?view plaincopy
?? ??? #include?<stdio.h>?? #include?<dirent.h>?? int?main(int?argc,?char?*?argv[])?? {?? ????struct?dirent?*ptr;?????? ????DIR?*dir;?? ????dir=opendir("./file");?? ????printf("文件列表:\n");?? ????while((ptr=readdir(dir))!=NULL)?? ????{?? ??? ?????????? ????????if(ptr->d_name[0]?==?'.')?? ????????????continue;?? ????????printf("%s\n",ptr->d_name);?? ????}?? ????closedir(dir);?? ????return?0;?? }??
C++ 版本
[cpp]?view plaincopy
#include?<iostream>?? #include?<vector>?? #include?<string>?? #include?<dirent.h>?? using?namespace?std;?? ?? int?main(int?argc,?char?*?argv[])?? {?? ????struct?dirent?*ptr;?????? ????DIR?*dir;?? ????string?PATH?=?"./file";?? ????dir=opendir(PATH.c_str());??? ????vector<string>?files;?? ????cout?<<?"文件列表:?"<<?endl;?? ????while((ptr=readdir(dir))!=NULL)?? ????{?? ??? ?????????? ????????if(ptr->d_name[0]?==?'.')?? ????????????continue;?? ?????????? ????????files.push_back(ptr->d_name);?? ????}?? ?????? ????for?(int?i?=?0;?i?<?files.size();?++i)?? ????{?? ????????cout?<<?files[i]?<<?endl;?? ????}?? ?? ????closedir(dir);?? ????return?0;?? } ?
_finddata_t 的使用??
那么到底如何查找文件呢?我們需要一個結構體和幾個大家可能不太熟悉的函數。這些函數和結構體在<io.h>的頭文件中,結構體為struct _finddata_t ,函數為_findfirst、_findnext和_fineclose。具體如何使用,我會慢慢講來~
? ? ? ? 首先講這個結構體吧~ struct _finddata_t ,這個結構體是用來存儲文件各種信息的。說實話,這個結構體的具體定義代碼,我沒有找到,不過還好,文檔里面在_find里有比較詳細的成員變量介紹。我基本上就把文檔翻譯過來講吧:
? ? ? ? unsigned atrrib:文件屬性的存儲位置。它存儲一個unsigned單元,用于表示文件的屬性。文件屬性是用位表示的,主要有以下一些:_A_ARCH(存檔)、_A_HIDDEN(隱藏)、_A_NORMAL(正常)、_A_RDONLY(只讀)、_A_SUBDIR(文件夾)、_A_SYSTEM(系統)。這些都是在<io.h>中定義的宏,可以直接使用,而本身的意義其實是一個無符號整型(只不過這個整型應該是2的幾次冪,從而保證只有一位為1,而其他位為0)。既然是位表示,那么當一個文件有多個屬性時,它往往是通過位或的方式,來得到幾個屬性的綜合。例如只讀+隱藏+系統屬性,應該為:_A_HIDDEN | _A_RDONLY | _A_SYSTEM 。
?? ? ? time_t time_create:這里的time_t是一個變量類型(長整型?相當于long int?),用來存儲時間的,我們暫時不用理它,只要知道,這個time_create變量是用來存儲文件創建時間的就可以了。
? ? ? ? time_t time_access:文件最后一次被訪問的時間。
? ? ? ? time_t time_write:文件最后一次被修改的時間。
? ? ? ? _fsize_t size:文件的大小。這里的_fsize_t應該可以相當于unsigned整型,表示文件的字節數。
? ? ? ? char name[_MAX_FNAME]:文件的文件名。這里的_MAX_FNAME是一個常量宏,它在<stdlib.h>頭文件中被定義,表示的是文件名的最大長度。
? ? ? ? 以此,我們可以推測出,struct _finddata_t ,大概的定義如下:
? ? ? ? struct _finddata_t
? ? ? ? {
? ? ? ? ? ? ?unsigned attrib;
? ? ? ? ? ? ?time_t time_create;
? ? ? ? ? ? ?time_t time_access;
? ? ? ? ? ? ?time_t time_write;
? ? ? ? ? ? ?_fsize_t size;
? ? ? ? ? ? ?char name[_MAX_FNAME];
? ? ? ? };
? ? ? ? 前面也說了,這個結構體是用來存儲文件信息的,那么如何把一個硬盤文件的文件信息“存到”這個結構體所表示的內存空間里去呢?這就要靠_findfirst、_findnext和_fineclose三個函數的搭配使用了。
? ? ? ? 首先還是對這三個函數一一介紹一番吧……
? ? ? ? long _findfirst( char *filespec, struct _finddata_t *fileinfo );
? ? ? ? 返回值:如果查找成功的話,將返回一個long型的唯一的查找用的句柄(就是一個唯一編號)。這個句柄將在_findnext函數中被使用。若失敗,則返回-1。
? ? ? ? 參數:
? ? ? ? filespec:標明文件的字符串,可支持通配符。比如:*.c,則表示當前文件夾下的所有后綴為C的文件。
? ? ? ? fileinfo :這里就是用來存放文件信息的結構體的指針。這個結構體必須在調用此函數前聲明,不過不用初始化,只要分配了內存空間就可以了。函數成功后,函數會把找到的文件的信息放入這個結構體中。
? ? ? ? int _findnext( long handle, struct _finddata_t *fileinfo );
? ? ? ? 返回值:若成功返回0,否則返回-1。
? ? ? ? 參數:
? ? ? ? handle:即由_findfirst函數返回回來的句柄。
? ? ? ? fileinfo:文件信息結構體的指針。找到文件后,函數將該文件信息放入此結構體中。
? ? ? ?int _findclose( long handle );
? ? ? ? 返回值:成功返回0,失敗返回-1。
? ? ? ? 參數:
? ? ? ? handle:即由_findfirst函數返回回來的句柄。
? ? ? ? fileinfo:文件信息結構體的指針。找到文件后,函數將該文件信息放入此結構體中。
? ? ? ?int _findclose( long handle );
? ? ? ? 返回值:成功返回0,失敗返回-1。
? ? ? ? 參數:
? ? ? ? handle :_findfirst函數返回回來的句柄。
? ? ? ? 大家看到這里,估計都能猜到個大概了吧?先用_findfirst查找第一個文件,若成功則用返回的句柄調用_findnext函數查找其他的文件,當查找完畢后用,用_findclose函數結束查找。恩,對,這就是正確思路。下面我們就按照這樣的思路來編寫一個查找C:\WINDOWS文件夾下的所有exe可執行文件的程序。
? ? ? ? #include <stdio.h>
? ? ? ? #include <io.h>
? ? ? ? const char *to_search="C:\\WINDOWS\\*.exe"; ? ? ? ?//欲查找的文件,支持通配符
? ? ? ? int main()
? ? ? ? {
? ? ? ? ? ? ?long handle; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//用于查找的句柄
? ? ? ? ? ? ?struct _finddata_t fileinfo; ? ? ? ? ? ? ? ? ? ? ? ? ?//文件信息的結構體
? ? ? ? ? ? ?handle=_findfirst(to_search,&fileinfo); ? ? ? ? //第一次查找
? ? ? ? ? ? ?if(-1==handle)return -1;
? ? ? ? ? ? ?printf("%s\n",fileinfo.name); ? ? ? ? ? ? ? ? ? ? ? ? //打印出找到的文件的文件名
? ? ? ? ? ? ?while(!_findnext(handle,&fileinfo)) ? ? ? ? ? ? ? //循環查找其他符合的文件,知道找不到其他的為止
? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? printf("%s\n",fileinfo.name);
? ? ? ? ? ? }
? ? ? ? ? ? ?_findclose(handle); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//別忘了關閉句柄
? ? ? ? ? ? ?system("pause");
? ? ? ? ? ? ?return 0;
? ? ? ? }
? ? ? ? 當然,這個文件的查找是在指定的路徑中進行,如何遍歷硬盤,在整個硬盤中查找文件呢?大家可以在網絡上搜索文件遞歸遍歷等方法,這里不再做進一步介紹。
? ? ? ? 程序的末尾用了一個system函數。這個與程序本身并沒有影響,和以前介紹給大家的使用getchar()函數的作用相同,只是為了暫停一下,讓我們能看到命令提示符上輸出的結果而已。不過system函數本身是一個非常強大的函數。大家可以查查MSDN看看~ 簡單來說,它是一個C語言與操作系統的相互平臺,可以在程序里通過這個函數,向操作系統傳遞command
總結
以上是生活随笔為你收集整理的C++ 从文件夹中读取文件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。