Linux 下c语言编程所引用 LINUX系统下的一个头文件,在这个目录下/usr/include 为了获取某文件夹目录内容,所使用的结构体。 引用头文件#include<dirent.h> 结构体说明 struct dirent { long d_ino; /* inode number 索引节点号 */ off_t d_off; /* offset to this dirent 在目录文件中的偏移 */ unsigned short d_reclen; /* length of this d_name 文件名长 */ unsigned char d_type; /* the type of d_name 文件类型 */ char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */ } 相关函数 opendir(),readdir(),closedir(); 使用实例 #include <stdio.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #ifndef DT_DIR #error "DT_DIR not defined, maybe d_type not a mumber of struct dirent!" #endif int main(int argc, char*argv[]) { staticchar dot[] =".", dotdot[] =".."; constchar*name; DIR *dirp; struct dirent *dp; if (argc ==2) name = argv[1]; else name = dot; dirp = opendir(name); if (dirp == NULL) { (void)fprintf(stderr, "%s: opendir(): %s: %s\n", argv[0], name, strerror(errno)); exit(errno); } while ((dp = readdir(dirp)) != NULL) { if (dp->d_type == DT_DIR) if ( strcmp(dp->d_name, dot) && strcmp(dp->d_name, dotdot) ) (void)printf("%s/\n", dp->d_name); } (void)closedir(dirp); return (0); }