c linux 文件文件夹遍历

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

void listFiles(const char *path) {
    struct dirent *entry;
    DIR *dir = opendir(path);

    if (dir == NULL) {
        perror("Error opening directory");
        exit(EXIT_FAILURE);
    }

    while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }
        // Check if the entry is a directory and recursively list its contents
        if (entry->d_type == DT_DIR) {
            char subpath[256];
            printf("dir %s\n", entry->d_name);
            snprintf(subpath, sizeof(subpath), "%s/%s", path, entry->d_name);
            listFiles(subpath);
        }
        else printf("file %s\n", entry->d_name);
    
    }

    closedir(dir);
}

int main() {
    const char *folderPath = "."; // Change this to the desired folder path
    listFiles(folderPath);

    return 0;
}
相关推荐
import_random29 分钟前
[机器学习]xgboost和lightgbm(区别)
算法
梁辰兴30 分钟前
数据结构:实验7.3Huffman树与Huffman编码
数据结构·c++·算法·c
wuqingshun31415933 分钟前
经典算法 最长单调递增子序列
java·c++·算法·蓝桥杯·机器人
企鹅chi月饼37 分钟前
动态规划问题,下降路径最小和(dp初始化问题,状态压缩),单词拆分(回溯法+剪枝+记忆化),substr函数
算法·动态规划
苯酸氨酰糖化物43 分钟前
计算机毕业设计--基于深度学习(U-Net与多尺度ViT)的车牌模糊图像修复算法设计与实现(含Github代码+Web端在线体验界面)
深度学习·算法·课程设计
@t.t.43 分钟前
Docker容器资源控制--CGroup
linux·运维·docker·容器·云计算
豆沙沙包?1 小时前
2025年- H13-Lc120-189.轮转数组(普通数组)---java版
java·算法·排序算法
不想当程序猿_1 小时前
Centos 7系统 宝塔部署Tomcat项目(保姆级教程)
linux·redis·centos·tomcat·minio·宝塔
吴声子夜歌1 小时前
Linux运维——Vim基础
linux·运维·vim