温馨提示×

debian readdir需要什么头文件

小樊
36
2026-10-16 01:58:20
栏目: 智能运维

在 Debian(以及一般的 Linux / POSIX 系统) 中,使用 readdir 需要包含以下头文件:

#include <dirent.h>

基本示例

#include <stdio.h>
#include <dirent.h>

int main() {
    DIR *dir = opendir(".");
    if (!dir) {
        perror("opendir");
        return 1;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        printf("%s\n", entry->d_name);
    }

    closedir(dir);
    return 0;
}

相关说明

  • readdir 函数原型:
    struct dirent *readdir(DIR *dirp);
    
  • 需要的类型和函数:
    • DIR → <dirent.h>
    • struct dirent → <dirent.h>
    • opendir, closedir → <dirent.h>

编译

gcc test.c -o test

如果你指的是 readdir_r(线程安全版本)或 getdents,可以告诉我,我可以补充对应头文件和区别。

0 踩