有一些问题试图想出一个好方法来拥有一个只选择 wav 文件并专门返回这些文件的字符串的目录遍历器,例如只是wav文件。我可以停止 dirent.h 在子目录中查找,我很确定我只需要一个 if 语句,它只允许字符串中带有 .wav 的文件
到目前为止我的代码看起来是这样的,我只是扩展了我在头文件的 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;
}
任何帮助表示赞赏,我不是 super 困难 - 但到目前为止我还没有弄明白。
最佳答案
我在 github 上有您正在寻找的解决方案.
关于c++ - Dirent.h - 专门获取具有特定扩展名的文件的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336011/