2013-06-23 36 views
2

我試圖在C++中創建一個應用程序來搜索磁盤上的所有.wav文件(包括所有子文件夾及其子文件夾等等,整個磁盤搜索,如防病毒搜索在Avast等上),並將它們存儲在一個字符串中,然後保存到文本文件中。我有麻煩找出如何實際做到這一點,我可以使用一些幫助。C++查找所有.wav文件

這是我迄今爲止所做的,它只搜索C盤的根文件夾,我不知道現在該做什麼。我知道我應該使用某種類型的循環,但我不知道如何實際做到這一點,沒有合乎邏輯的方式可以這麼做。

void finddata() 
{ 
    MessageBox(NULL, "finddata called", "Started", MB_OK | MB_ICONINFORMATION); 
    WIN32_FIND_DATA FindFileData; // A pointer to the WIN32_FIND_DATA structure that receives information about a found file or directory. 
    HANDLE hFind; // Used to check the return value 
    hFind = FindFirstFile("C://*.wav", &FindFileData); 
    if(hFind == INVALID_HANDLE_VALUE) 
    { 
     MessageBox(NULL, "INVALID_HANDLE_VALUE", "Failed", MB_OK | MB_ICONINFORMATION); 
    } 
    else 
    { 
     string file; 
     file = FindFileData.cFileName; 
     file.append("|"); 
     MessageBox(NULL, file.c_str(), "YO", MB_OK | MB_ICONASTERISK); // string.c_str() to conv to LPCSTR 
     FindClose(hFind); 
    } 
} 

MessageBox在最後只是作爲一個測試,我將在稍後刪除它。我目前正在學習C++,這是一個初學者的項目,所以我可以得到它的一個竅門。

此外,字符串數據類型的大小是否有任何限制?謝謝。

+0

您在使用Windows作爲OS? –

+0

您可以使用std :: string :: max_size來查看std字符串的最大大小。 –

+0

是的,Windows 7 64位 –

回答

2

顯然,你確實需要調用一個FindNextFile,並重復使用某種類型的循環,直到它返回一個「不再有文件」的返回碼。

要搜索整個磁盤,您需要在「當前目錄」(根目錄)中查找目錄,並且爲每個目錄搜索它 - 可以通過遞歸調用finddata(添加目錄的名稱作爲函數的參數),或者在代碼中實現一個堆棧以跟蹤您所在的目錄以及要「回到」下一個級別的目錄。

我故意不寫代碼給你,而是描述你需要做什麼,因爲你是一個學習編程。我在1985年左右做過這類事情。

1

遞歸就是這樣。

這裏是一個可能實現(靈感,而是做你自己的方式):

// Filtering by the given extension 
void AppendFile(list<string>& fileList, const string& directory, const string& fileName, const string& extFilter) 
{ 
    string fileExtension = fileName.substr(fileName.find_last_of(".") + 1); 
    if (!fileExtension.empty()) 
    { 
     // Extension doesn't match: return 
     if (extFilter.find(fileExtension) == string::npos) 
      return; 
    } 
    // If we have a match: append file to list 
    fileList.push_back(directory + fileName); 
} 

// Getting the files of the given extension recursively (or not) 
void GetFiles(list<string>& fileList, string directory, const string& extFilter, bool recursively = true) 
{ 
    string filePath; 

    directory += "\\"; 
    string filter = directory + "*"; 

    WIN32_FIND_DATA FindFileData; 
    HANDLE hFind = FindFirstFile(filter.c_str(), &FindFileData); 

    if (hFind == INVALID_HANDLE_VALUE) return; 

    do 
    { 
     // If we find a (sub-)directory 
     if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
     { 
      // Here comes the recursive call 
      if ((recursively) && (lstrcmp(FindFileData.cFileName, TEXT(".")) != 0) && (lstrcmp(FindFileData.cFileName, TEXT("..")) != 0)) 
       // The 'GetFiles()' calls itself on the sub-directory 
       GetFiles(fileList, directory + FindFileData.cFileName, extFilter); 
     } 
     else 
      // If we find a file: simply append it to the list 
      AppendFile(fileList, directory, FindFileData.cFileName, extFilter); 
    } 
    while (FindNextFile(hFind, &FindFileData) != 0); 

    FindClose(hFind); 
} 

用法:

list<string> fileList; 
GetFiles(fileList, "C:\\root_dir", "wav"); 

for(list<string>::iterator it = fileList.begin(); it != fileList.end(); ++it) 
{ 
    cout << (*it) << endl; 
}