1. 程式人生 > >C++用 _findfirst 和 _findnext 查詢檔案

C++用 _findfirst 和 _findnext 查詢檔案

複製程式碼
#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++ _findfirst _findnext 查詢檔案

#include <io.h> #include <iostream> #include <fstream> using namespace std; bool transfer(string fileName, int exeNum ); void dfsFolder(

_findfirst _findnext 查詢檔案(轉)

_finddata_t struct _finddata_t   是用來儲存檔案各種資訊的結構體。定義如下: struct _finddata_t { unsigned attrib; time_t time_create; time_t time_access; time_t time_write; _

C#Odbc、Oledb查詢ExcelCSV

/// <summary> /// Oledb查詢Excel /// </summary> public static void QueryExcelToOledb() { //檔案

原生nodefshttp完成檔案上傳到伺服器

var http=require("http"); var fs=require("fs"); http.createServer(function(req,res){ res.writeHead(200,{"Content-type":"text/html;charset=UTF-8","

mongoredis查詢排行榜、統計活躍使用者

  nosql資料庫能解決關係型資料庫遇到的效能和擴充套件性的問題,本部落格將以mongodb和redis兩種nosql資料庫為基礎,簡單的介紹下面兩個業務場景的解決方案:   1.查詢排行榜(以當日總步數排名為例,查詢排名前200的使用者); 2.統計活躍使用者數(統計某個移動端app軟體在各個下載渠道的活

C#加密解密PDF檔案

Spire.PDF具有強大的功能,可以使用密碼加密和解密PDF文件。我們已經通過程式碼進行了一些調整以保護PDF檔案。如果您在之前使用Spire.PDF版本3.9.421,請參考這裡的教程解密PDF,加密PDF在C#。從Spire.PDF v 3.9.421開始,您應該使用本文中提供的新方法。此示例

C++new不用new建立類物件區別

new建立類物件,使用完後需使用delete刪除,跟申請記憶體類似。所以,new有時候又不太適合,比如在頻繁呼叫場合,使用區域性new類物件就不是個好選擇,使用全域性類物件或一個經過初始化的全域性類指標似乎更加高效。 一、new建立類物件與不new區別 下面是自

C++】C++new不用new建立類物件區別

起初剛學C++時,很不習慣用new,後來看老外的程式,發現幾乎都是使用new,想一想區別也不是太大,但是在大一點的專案設計中,有時候不使用new的確會帶來很多問題。 當然這都是跟new的用法有關的。new建立類物件,使用完後需使用delete刪除,跟申請記憶體類似。所以

Asynchronous Programming with async and await (C#)asyncawait實現非同步程式設計

You can avoid performance bottlenecks and enhance the overall responsiveness of your application by using asynchronous programming. Howe

c++cingetline實現輸入回車結束輸入

今天做一道測試題遇到了一個麻煩,我想要先讀入一個字串,再讀入一個整數,迴圈往復,直到字串是空,也就是說回車鍵結束迴圈。 最開始的想法是: string s;int d; while(cin>>s){ cin>>d; // prece

Linux裡利用grepfind查詢檔案內容

從檔案內容查詢匹配指定字串的行: $ grep “被查詢的字串” 檔名 例子:在當前目錄裡第一級資料夾中尋找包含指定字串的.in檔案 grep “thermcontact” /.in 從檔案內容

C++fstream,ifstreamoutstream讀取檔案最後一行注意

char cntext[1024] = {0};  fstream cnText("E:\\Work\\Diagnose\\Chrysler\\BAIC\\CN_TEXT.txt");  //cnText.open(("E:\\Work\\Diagnose\\Chrysle

C# NPOI儲存為Excel檔案

首先下載NPOI元件,版本:NPOI 2.0 下載後新增引用(這裡有.net2.0版和4.0版的,按需引用) 使用方法如下: /// <summary> /// 儲存成excel(NPOI方式) ///

C++new不用new建立類物件的區別

     起初剛學C++時,很不習慣用new,後來看老外的程式,發現幾乎都是使用new,想一想區別也不是太大,但是在大一點的專案設計中,有時候不使用new的確會帶來很多問題。當然這都是跟new的用法有關的。new建立類物件,使用完後需使用delete刪除,跟申請記憶

C#serialPortchart控制元件實現簡單波形繪製

先看最終的效果圖: 主要實現功能是將串列埠傳送過來的資料按波形顯示 注:本例是以串列埠除錯助手和虛擬串列埠VSPD軟體模擬串列埠傳送資料的,詳細說明見下文 說明: serialPort的ReadByte()方法用於從System.IO.Ports.SerialPort輸入

C#中建立讀取XML檔案

專案中需要將前臺頁面中的資訊儲存下來並存儲為xml檔案格式到資料庫中去。因此我先在這裡通過一個小例項來學習xml的建立與讀取。 1.建立簡單的XML檔案 為了便於測試,我們首先建立控制檯應用程式,專案命名為CreateXml,Program.cs程式碼如下: u

c# 委託事件實現不同窗體間的通訊(一)

 C# 中的“事件”是當物件滿足一定條件,發生某些事情時,類向該類的客戶提供通知的一種方法。使用事件,擁有該事件的物件不必知道需要通知誰,一旦滿足了某個條件,將自動呼叫該事件,正確通知每個需要通知的物件。通過使用事件,提高了程式的模組化程度。    例子:通過form1開啟f

linux下c語言遞迴法查詢檔案,並列印絕對地址

c語言遞迴法查詢指定目錄下的檔案或目錄   本程式碼是個人學習過程中寫下的小練筆,如果您發現問題,歡迎指正。 編譯環境:linux+gcc, windows下尚未測試 #include<stdio.h> #include<string.h> #inc

C++new不用new建立類物件

1,new建立類物件,使用完後需使用delete刪除,跟申請記憶體類似。所以,new有時候又不太適合,比如在頻繁呼叫場合,使用區域性new類物件就不是個好選擇,使用全域性類物件或一個經過初始化的全域性類指標似乎更加高效。 2,非new建立類物件,建立方式,new物件指標作為

基於Linux c socket執行緒 實現的簡易聊天室之伺服器

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #