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

linux编程C++中根据指定文件后辍枚举指定目录下所有匹配文件

Posted on 2024-12-07 13:07:03 by 主打一个C++

封装函数-根据指定后辍、枚举指定目录中的所有后辍匹配的文件。

测试打印代码如下:

#include <iostream>
#include <string>
#include <dirent.h>
int GetFileExtensionCount(const char* _fileDir, const char* _extension) {
    DIR* dir = opendir(_fileDir);
    if (dir == nullptr) {
        return 1;
    }

    dirent* entry = nullptr;
    while ((entry = readdir(dir)) != nullptr) {
        if (entry->d_type == DT_REG) { // 确保只处理文件
            const char* ext = strrchr(entry->d_name, '.');
            printf("ext: %s\n", ext);
            if (ext && strcmp(ext, _extension) == 0) {
                printf("%s\n", (std::string(_fileDir) + entry->d_name).c_str());
            }
        }
    }

    closedir(dir);
    return 0;
}
int main() {

    return GetFileExtensionCount("./", ".txt");
}


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

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



wechat +447752296473
wechat cpp-blog