词条 | dirent |
释义 | 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\", 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/\", dp->d_name); } (void)closedir(dirp); return (0); } |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。