C++遍歷資料夾下所有的wav檔案(支援windows和Linux)
阿新 • • 發佈:2018-11-17
直接上程式碼:
#include <iostream> #include <string> #include <vector> #include <fstream> #include <string.h> #include <dirent.h> using namespace std; // Find all files ext is wav void getFileNames(string path,vector<string>& filenames) { DIR *pDir; struct dirent* ptr; if(!(pDir = opendir(path.c_str()))){ cout<<"Folder doesn't Exist!"<<endl; return; } while((ptr = readdir(pDir))!=0) { if(0 == strcmp(strchr(ptr->d_name, '.'), ".wav")){ filenames.push_back(path + "/" + ptr->d_name); } } closedir(pDir); } void fileTest(){ int i = 0; string path = "H:\\audio"; vector<string> files; getFileNames(path, files); int file_numbers = files.size(); for (i = 0; i < file_numbers; ++i) { char file_path[100]; strcpy(file_path, files[i].c_str()); //strcpy_s( file_path, files[i].c_str()); printf("%-4d", i+1); cout<<" "<< file_path <<endl; } } int main() { fileTest(); }