欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

linux高编IO-------opendir、closedir、readdir

发布时间:2024/4/17 52 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux高编IO-------opendir、closedir、readdir 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

包含头文件

#include <dirent.h> #include <sys/types.h>

opendir

/**************************** 功能:打开目录文件* 参数:目录名* 返回值:成功返回指向目录文件的指针,失败返回NULL,并设置errno* ************************/ DIR *opendir(const char pathname);

 

closedir

/**************************** 功能:关闭目录文件* 参数:指向目录文件的指针* 返回值:成功返回0,失败返回-1* ************************/ int closedir(DIR *dirp);

readdir

/**************************** 功能:读取目录文件* 参数:指向目录文件的指针* 返回值:成功目录信息的结构体,失败返回NULL,并设置errno* ************************/ struct dirent *readdir(DIR *dirp);struct dirent {ino_t d_ino; /* inode number */off_t d_off; /* offset to the next dirent */unsigned short d_reclen; /* length of this record */unsigned char d_type; /* type of file; not supportedby all file system types */char d_name[256]; /* filename */ };

例子:

/************************查看/etc目录所有文件**********************/ #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/types.h>#define PAT "/etc"int main() {//1.定义目录指针,结构体DIR *dp ;struct dirent *cur ;//2.打开目录文件dp = opendir(PAT);if(dp == NULL){perror("opendir()");exit(1);}//3.读目录内容while((cur = readdir(dp)) != NULL){puts(cur->d_name);}//4.关闭目录文件 close(dp);exit(0); }

 

转载于:https://www.cnblogs.com/muzihuan/p/5279620.html

总结

以上是生活随笔为你收集整理的linux高编IO-------opendir、closedir、readdir的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。