文章详情
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