L o a d i n g . . .
主打一个C++
文章详情

C++中linux开发获取指定目录下所有指定文件

Posted on 2023-03-01 07:31:28 by 主打一个C++

//函数封装,参数(要遍历的目录,指定后辍名,返回列表)

bool broadcast_server::getFiles(const std::string& _directory, const std::string& _extension, std::vector<std::string>& _list) {
    DIR* dir;
    struct dirent* ent;

    // 打开目录
    if ((dir = opendir(_directory.c_str())) != nullptr) {
        // 遍历目录
        while ((ent = readdir(dir)) != nullptr) {
            // 检查是否是文件并且以.php结尾
            if (ent->d_type == DT_REG) { // 确保是常规文件
                std::string fileName = ent->d_name;
                if (fileName.size() > 4 && fileName.substr(fileName.size() - 4) == _extension) {
                    _list.push_back(fileName);
                }
            }
        }
        closedir(dir); // 关闭目录
    }
    else {
        std::cerr << "无法打开目录: " << strerror(errno) << std::endl;
        return false;
    }

    return true;
}


*转载请注明出处:原文链接:https://cpp.vin/page/98.html

作者近期文章
  • 随手笔记
  • 主打一个C++   2025-01-11 20:02:01
  • 都2000000025年了。还有不能随意访问guthub的,仔细看。在国内其实是可以正常访问的,gfw并没屏蔽。这里给出其中一个简单直接的方法稳定访问。1. 随便百度一个”dn
提示
×
确定
数据库执行: 8次 总耗时: 0.02s
页面加载耗时: 



wechat +447752296473
wechat cpp-blog