c++ - Dirent.h - 专门获取具有特定扩展名的文件的字符串

标签 c++

有一些问题试图想出一个好方法来拥有一个只选择 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/

相关文章:

c++ - OpenGL纹理

c# - QuickFIX/n - 启动器在登录阶段反复抛出错误

Ubuntu 中的 C++ 手册页

c++ - 如何将C++字符串放入char数组

c++ - 如何让C++随机按键

c++ - 使用此 CMake 链接命令仍然需要针对 OpenMP::OpenMP_CXX 的显式链接吗?

c++ - 始终分配 vs 检查相等性并分配

c++ - 链接@_Znam 和@_Znwm

c++ - 在 CPP 中初始化一个 2 Dimensional vector<int>

c++ - std::map 准备但不插入