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;
}
相关推荐
Eyfcom2 小时前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
kyle~4 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
老花眼猫4 小时前
C语言绘图方法:(10)椭圆缩放旋转
c语言·经验分享·青少年编程·课程设计
忆挽篱笙歌4 小时前
systemV进程通信
linux·运维·服务器
牢姐与蒯5 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好6 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
电科_银尘7 小时前
【大模型算法与工程 02】线性回归与逻辑回归:从损失函数到决策边界
算法·逻辑回归·线性回归
玄昌盛不会编程8 小时前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
无奈笑天下10 小时前
银河麒麟桌面操作系统上磁盘无法自动挂载问题处理
linux·运维·服务器