c语言 依次读取文件夹,c++按顺序读取目录内文件

背景

按顺序读取目录内文件

之前使用C语言的opendir函数读取目录内文件,发现不是按照名字排序的,看解释返回的列表貌似是根据到某个存储节点的距离来排序的,所以找到了这个函数scandir。

代码

#include

int scandir(const char *name, struct dirent ***namelist,int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **));

int alphasort(const void *a, const void *b);

int versionsort(const void *a, const void *b);

param 1:目录的名字

param 2:返回的目录内经过某种排序的文件列表

param 3:某种过滤规则,自定义

param 4:某种排序规则,可以自定义,也有两个给定的

注意:这里第二个参数是内部malloc内存,需要在外部free掉

附上代码:

#include

#include

#include

#include

#include

using namespace std;

int fileNameFilter(const struct dirent *cur) {

std::string str(cur->d_name);

if (str.find(".bin") != std::string::npos) {

return 1;

}

return 0;

}

std::vector<:string> getDirBinsSortedPath(std::string dirPath) {

struct dirent **namelist;

std::vector<:string> ret;

int n = scandir(dirPath.c_str(), &namelist, fileNameFilter, alphasort);

if (n < 0) {

return ret;

}

for (int i = 0; i < n; ++i) {

std::string filePath(namelist[i]->d_name);

ret.push_back(filePath);

free(namelist[i]);

};

free(namelist);

return ret;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用C语言实现遍历读取多个文件文件的代码,注释已经加上: ```c #include <stdio.h> // 包含头文件 #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> void traverseFolders(char *path) { // 定义遍历文件函数 DIR *dir; struct dirent *ent; struct stat st; char full_file_name[1024]; if ((dir = opendir(path)) == NULL) { // 打开文件 return; } while ((ent = readdir(dir)) != NULL) { // 读取文件内容 char *file_name = ent->d_name; // 获取文件名 sprintf(full_file_name, "%s/%s", path, file_name); // 获取文件完整路径 if (file_name[0] == '.') // 跳过隐藏文件 continue; if (stat(full_file_name, &st) == -1) // 获取文件信息 continue; if (S_ISDIR(st.st_mode)) { // 判断是否为文件 traverseFolders(full_file_name); // 递归遍历子文件 } else { printf("%s\n", full_file_name); // 输出文件路径 FILE *file = fopen(full_file_name, "r"); // 打开文件 char line[1024]; while (fgets(line, 1024, file)) { // 逐行读取文件内容 // 处理文件内容 } fclose(file); // 关闭文件 } } closedir(dir); // 关闭文件 } int main() { char path[] = "/path/to/folder"; // 定义文件路径 traverseFolders(path); // 调用遍历文件函数 return 0; } ``` 代码注释: 1. `#include <stdio.h>`:C语言标准输入输出的头文件。 2. `#include <stdlib.h>`:C语言标准库的头文件。 3. `#include <dirent.h>`:C语言文件操作的头文件。 4. `#include <sys/stat.h>`:C语言文件信息的头文件。 5. `#include <string.h>`:C语言字符串操作的头文件。 6. `void traverseFolders(char *path)`:定义遍历文件函数,参数为文件路径。 7. `DIR *dir`:文件指针。 8. `struct dirent *ent`:文件内容指针。 9. `struct stat st`:文件信息结构体。 10. `char full_file_name[1024]`:定义文件完整路径数组。 11. `if ((dir = opendir(path)) == NULL)`:打开文件,如果打开失败则返回。 12. `while ((ent = readdir(dir)) != NULL)`:读取文件内容,如果读取完毕则返回。 13. `char *file_name = ent->d_name`:获取文件名。 14. `sprintf(full_file_name, "%s/%s", path, file_name)`:获取文件完整路径。 15. `if (file_name[0] == '.') continue`:跳过隐藏文件。 16. `if (stat(full_file_name, &st) == -1) continue`:获取文件信息,如果获取失败则返回。 17. `if (S_ISDIR(st.st_mode))`:判断是否为文件。 18. `traverseFolders(full_file_name)`:递归遍历子文件。 19. `printf("%s\n", full_file_name)`:输出文件路径。 20. `FILE *file = fopen(full_file_name, "r")`:打开文件。 21. `char line[1024]`:定义字符串数组。 22. `while (fgets(line, 1024, file))`:逐行读取文件内容,如果读取完毕则返回。 23. `fclose(file)`:关闭文件。 24. `closedir(dir)`:关闭文件。 25. `int main()`:主函数。 26. `char path[] = "/path/to/folder"`:定义文件路径。 27. `traverseFolders(path)`:调用遍历文件函数。 28. `return 0`:返回0,表示程序正常结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值