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;
}
相关推荐
Xiaoᴗo.2 分钟前
C语言2.0---------
c语言·开发语言·数据结构
ghie90903 分钟前
MATLAB 解线性方程组的迭代法
开发语言·算法·matlab
m0_743106464 分钟前
【浙大&南洋理工最新综述】Feed-Forward 3D Scene Modeling(二)
人工智能·算法·计算机视觉·3d·几何学
Brilliantwxx5 分钟前
【数据结构】排序算法的神奇世界(下)
c语言·数据结构·笔记·算法·排序算法
进击的荆棘5 分钟前
递归、搜索与回溯——二叉树中的深搜
数据结构·c++·算法·leetcode·深度优先·dfs
j_xxx404_8 分钟前
面试官灵魂拷问:Linux软链接与硬链接到底有什么区别?(附底层Inode级深度图解)
linux·运维·服务器
人道领域9 分钟前
【LeetCode刷题日记】:151翻转字符串的单词(两种解法)
java·开发语言·算法·leetcode·面试
会编程的土豆13 分钟前
【日常做题】栈 中缀前缀后缀
开发语言·数据结构·算法
进击的荆棘14 分钟前
递归、搜索与回溯——回溯
数据结构·c++·算法·leetcode·dfs
励志的小陈18 分钟前
数据结构--二叉树(链式结构、C语言实现、层序遍历)
c语言·数据结构