4
有幾個問題試圖想出一個好的方法來創建一個只選擇wav文件並僅返回這些文件的字符串的目錄遍歷器。只是wav文件。我可以停止在子目錄dirent.h看,我敢肯定,我只需要一個if語句,只允許文件與.wav字符串Dirent.h - 獲得具有特定擴展名的文件的字符串
到目前爲止我的代碼看起來像這樣,我只是擴展了幾個例子,見過的頭文件中的wiki頁面:
ifstream fin;
string dir, filepath;
DIR *dp;
struct dirent *dirp;
struct stat filestat;
cout << "dir to get files of: " << flush;
getline(cin, dir);
dp = opendir(dir.c_str());
if (dp == NULL){
cout << "Error(" << errno << ") opening " << dir << endl;
}
while ((dirp = readdir(dp))){
filepath = dir + "/" + dirp->d_name;
if (stat(filepath.c_str(), &filestat)) continue;
if (S_ISDIR(filestat.st_mode)) continue;
fin.open(filepath.c_str());
stringVec.push_back(filepath);
fin.close();
}
closedir(dp);
for(int i=0;i<stringVec.size();i++){
cout << stringVec[i] << endl;
}
任何幫助表示讚賞,我是它不是超級難 - 但到目前爲止,我一直沒能弄明白。
使用http://stackoverflow.com/questions/874134/find-if-string-endswith-another-string-in-c檢查文件名是否以.wav結尾 – user786653 2012-04-26 15:03:32
使用'if(!S_ISREG(filestat。 ST_MODE))';你的'.wav'文件不是套接字,字符或塊特殊設備,FIFO等。之後,甚至在任何基於stat的檢查之前,檢查名稱的最後4個字符是否是'.wav '。 – 2012-04-26 15:43:35