【问题标题】:C++ Find all .wav filesC++ 查找所有 .wav 文件
【发布时间】:2013-06-20 10:05:57
【问题描述】:

我正在尝试在 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++,这是一个初学者项目,所以我可以掌握它。

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

【问题讨论】:

  • 你是用windows作为操作系统吗?
  • 您可以使用 std::string::max_size 查看 std 字符串的最大大小。
  • 是的,Windows 7 64 位
  • 这类问题一般都是通过递归来解决的。

标签: c++ file search directory


【解决方案1】:

显然,您确实需要一个FindNextFile 调用,并使用某种循环重复该调用,直到它返回“没有更多文件”返回码。

要搜索整个磁盘,您需要在“当前目录”(根目录)中查找目录,并为每个目录搜索到其中 - 您可以通过递归调用 finddata 来执行此操作(添加目录的名称作为函数的参数),或者在代码中实现一个堆栈来跟踪您所在的目录,以及下一级“返回”哪个目录。

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

【讨论】:

    【解决方案2】:

    递归是一种方式。

    这是一个可能的实现方式(仅供参考,但请按照自己的方式进行):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2021-12-22
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多