C++查找所有.wav文件

C++ Find all .wav files

本文关键字:wav 文件 查找 C++      更新时间:2023-10-16

我正试图在C++中创建一个应用程序,该应用程序将搜索磁盘上的所有.wav文件(包括所有子文件夹及其子文件夹等…,整个磁盘搜索,如Avast上的防病毒搜索等),并将其存储在字符串中,稍后保存到文本文件中。我很难弄清楚如何真正做到这一点,我需要一些帮助。

这就是我到目前为止所做的,它只是搜索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++,这是一个初学者的项目,所以我可以掌握它。

此外,字符串数据类型的大小是否有任何限制?谢谢

显然,您确实需要一个FindNextFile调用,并使用某种循环重复该调用,直到它返回"不再有文件"返回代码。

然后,要搜索整个磁盘,您需要在"当前目录"(根目录)中查找目录,并对其中的每个目录进行搜索——您可以通过递归调用finddata(将目录名称作为参数添加到函数中)来做到这一点,也可以在代码中实现一个堆栈来跟踪您所在的目录,以及下一级应该"走回"哪个级别。

我有意不为你写代码,而是描述你需要做什么,因为你是学习编程的人。我在1985年左右做过这样的事情。

递归是一种方法。

这里有一个可能的实现(为了获得灵感,但要用自己的方式):

// 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;
}