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;
}
相关推荐
weixin_4612594123 分钟前
[C]C语言日志系统宏技巧解析
java·服务器·c语言
t1987512831 分钟前
Linux 上的 Tomcat 端口占用排查
linux·tomcat·firefox
OpenCSG1 小时前
电子行业AI赋能软件开发经典案例——某金融软件公司
人工智能·算法·金融·开源
小狗爱吃黄桃罐头1 小时前
正点原子[第三期]Arm(iMX6U)Linux移植学习笔记-12.1 Linux内核启动流程简介
linux·arm开发·学习
地衣君2 小时前
Ubuntu 配置使用 zsh + 插件配置 + oh-my-zsh 美化过程
linux·运维·ubuntu
dfsj660112 小时前
LLMs 系列科普文(14)
人工智能·深度学习·算法
2401_858286112 小时前
OS11.【Linux】vim文本编辑器
linux·运维·服务器·编辑器·vim
朱包林2 小时前
day27-shell编程(自动化)
linux·运维·服务器·网络·shell脚本
kaede2 小时前
Linux实现线程同步的方式有哪些?
linux·运维·云计算
德先生&赛先生2 小时前
Linux编程:2、进程基础知识
linux