C++查找所有.wav文件
C++ Find all .wav files
我正试图在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;
}
相关文章:
- 尝试使用来自 Win32 WASAPI C++的"捕获流"创建 wav 文件
- 如何从wav文件中获取采样率?
- 写入 wav 文件时定期创建静音
- 如何使用curl c ++源代码发布wav文件数据,如何使用c ++使用--data-binary?
- 为什么 XAudio2 仅在系统暂停时播放.wav文件?
- webRTC : 如何通过从WAV文件获得的样本将webRTC的VAD应用于音频
- WAV文件停止功能
- 如何从特定点播放.WAV文件(例如30秒)
- 通过C(或C )中的WAV文件循环
- 开放式阅读WAV文件lib
- 使用C++ / Python的wav文件的FFT
- 在mbed中打开的wav文件数量是否有限制
- 音频编程 SDL - 如何使用 wav 文件在命令行中指定频率切割
- 无法使用两种不同的频率填充 *.wav 文件的缓冲区
- 禁用在使用QAudioRecorder时创建wav文件
- 无论如何,我可以从WAV文件中打印Sox即可打印幅度值
- WAV文件的数据在-1和1之间,C ,SNDFILE
- 如何将.wav文件保存在MFC中(使用文件对话框)
- 分析.WAV 文件
- 用A4色调扩展.WAV文件