linux opendir和readdir的使用
来源:互联网 发布:廉价口红知乎 编辑:程序博客网 时间:2024/05/21 05:41
1 opendir
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
传入name路径,成功则返回非空DIR指针,否则返回NULL
2 readdir
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
readdir一般要配合opendir使用,readdir不是线程安全函数,代替他的有readdir_r。
readdir返回 struct dirent *指针,读完目录下所有文件时,返回NULL
如果系统支持readdir_r,建议用readdir_r , readdir_r成功返回0。
关于struct dirent结构体:
On Linux, the dirent structure is defined as follows:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* not an offset; see NOTES */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all filesystem types */
char d_name[256]; /* filename */
};
3 closedir
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
closedir配合opendir使用。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <dirent.h>#include <errno.h>//DIR *opendir(const char *name);#ifndef LOG_TRACE#define LOG_TRACE printf#define LOG_INFO(msg) \do{ \LOG_TRACE msg; \LOG_TRACE("[%s %d] \n",__FUNCTION__,__LINE__);\}while(0)#define LOG_ERROR(err_info) \do{ \LOG_TRACE err_info; \LOG_TRACE("[%s %d] \n",__FUNCTION__,__LINE__);\}while(0)#endifextern int errno;int open_dir_1(const char *pDirname){DIR * dirp = NULL; struct dirent * pDirent= NULL;if((NULL == pDirname) ||(0 == strlen(pDirname))){LOG_ERROR(("param error"));return -1;}dirp = opendir(pDirname);if (NULL == dirp){LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));return -1;}while (NULL != (pDirent = readdir(dirp))){if (pDirent->d_type == DT_DIR ){LOG_ERROR(("dir [%s] ",pDirent->d_name));}else if(pDirent->d_type == DT_REG){LOG_ERROR(("file [%s] ",pDirent->d_name));}}closedir(dirp);return 0;}int open_dir_2(const char *pDirname){DIR * dirp = NULL; struct dirent * pDirent= NULL;struct dirent *pStResult = NULL;if((NULL == pDirname) ||(0 == strlen(pDirname))){LOG_ERROR(("param error"));return -1;}dirp = opendir(pDirname);if (NULL == dirp){LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));return -1;}pDirent = (struct dirent *)malloc(sizeof(struct dirent));if(!pDirent){LOG_ERROR(("pDirent error"));closedir(dirp);return -1;}while (( 0== readdir_r(dirp,pDirent,&pStResult))&&(pStResult != NULL)){if (pDirent->d_type == DT_DIR ){LOG_ERROR(("dir [%s] ",pDirent->d_name));}else if(pDirent->d_type == DT_REG){LOG_ERROR(("file [%s] ",pDirent->d_name));}}closedir(dirp);return 0;}int main(){LOG_ERROR(("***********"));open_dir_1("/share/");LOG_ERROR(("------------"));open_dir_2("/share/");LOG_ERROR(("-++--+++--"));return 0;}
运行结果:
./a.out [19@gcc test_opendir_readdir.c[C
root@ubuntu:/share#
root@ubuntu:/share#
root@ubuntu:/share# gcc test_opendir_readdir.c
/a.out
***********[main 113]
file [zlib-1.2.8.tar.gz] [open_dir_1 54]
file [1.tmp] [open_dir_1 54]
file [log.c] [open_dir_1 54]
file [list.c] [open_dir_1 54]
dir [11] [open_dir_1 50]
file [test.out] [open_dir_1 54]
file [test_system_func.c] [open_dir_1 54]
file [a.out] [open_dir_1 54]
file [test_strncpy.c] [open_dir_1 54]
file [test_proc_partitions.c] [open_dir_1 54]
file [test.c] [open_dir_1 54]
file [New0001.c] [open_dir_1 54]
dir [curl-7.51.0] [open_dir_1 50]
file [test_ftok.c] [open_dir_1 54]
file [log.h] [open_dir_1 54]
file [test_gettimeofday.c] [open_dir_1 54]
file [csdn.c] [open_dir_1 54]
file [client.c] [open_dir_1 54]
file [1.txt] [open_dir_1 54]
file [test_opendir_readdir.c] [open_dir_1 54]
file [curl-7.51.0.tar.gz] [open_dir_1 54]
file [2.txt] [open_dir_1 54]
file [opendir.c] [open_dir_1 54]
dir [zlib-1.2.8] [open_dir_1 50]
file [123.rmvb] [open_dir_1 54]
dir [ffmpeg_learn] [open_dir_1 50]
file [test_sem.c] [open_dir_1 54]
file [test_list.c] [open_dir_1 54]
file [list.h] [open_dir_1 54]
file [server] [open_dir_1 54]
dir [ProFFmpeg] [open_dir_1 50]
dir [ffmpeg-3.1.6] [open_dir_1 50]
dir [yasm-1.3.0] [open_dir_1 50]
file [New0003.c] [open_dir_1 54]
dir [.] [open_dir_1 50]
dir [..] [open_dir_1 50]
file [ffmpeg-3.1.6.tar.gz] [open_dir_1 54]
file [types.h] [open_dir_1 54]
file [New0002.c] [open_dir_1 54]
file [simple.out] [open_dir_1 54]
file [server.c] [open_dir_1 54]
dir [learn] [open_dir_1 50]
file [simple_ffmpeg_player.c] [open_dir_1 54]
file [client] [open_dir_1 54]
dir [abc] [open_dir_1 50]
file [yasm-1.3.0.tar.gz] [open_dir_1 54]
file [output.yuv] [open_dir_1 54]
file [mySDLFirst.out] [open_dir_1 54]
------------[main 115]
file [zlib-1.2.8.tar.gz] [open_dir_2 100]
file [1.tmp] [open_dir_2 100]
file [log.c] [open_dir_2 100]
file [list.c] [open_dir_2 100]
dir [11] [open_dir_2 96]
file [test.out] [open_dir_2 100]
file [test_system_func.c] [open_dir_2 100]
file [a.out] [open_dir_2 100]
file [test_strncpy.c] [open_dir_2 100]
file [test_proc_partitions.c] [open_dir_2 100]
file [test.c] [open_dir_2 100]
file [New0001.c] [open_dir_2 100]
dir [curl-7.51.0] [open_dir_2 96]
file [test_ftok.c] [open_dir_2 100]
file [log.h] [open_dir_2 100]
file [test_gettimeofday.c] [open_dir_2 100]
file [csdn.c] [open_dir_2 100]
file [client.c] [open_dir_2 100]
file [1.txt] [open_dir_2 100]
file [test_opendir_readdir.c] [open_dir_2 100]
file [curl-7.51.0.tar.gz] [open_dir_2 100]
file [2.txt] [open_dir_2 100]
file [opendir.c] [open_dir_2 100]
dir [zlib-1.2.8] [open_dir_2 96]
file [123.rmvb] [open_dir_2 100]
dir [ffmpeg_learn] [open_dir_2 96]
file [test_sem.c] [open_dir_2 100]
file [test_list.c] [open_dir_2 100]
file [list.h] [open_dir_2 100]
file [server] [open_dir_2 100]
dir [ProFFmpeg] [open_dir_2 96]
dir [ffmpeg-3.1.6] [open_dir_2 96]
dir [yasm-1.3.0] [open_dir_2 96]
file [New0003.c] [open_dir_2 100]
dir [.] [open_dir_2 96]
dir [..] [open_dir_2 96]
file [ffmpeg-3.1.6.tar.gz] [open_dir_2 100]
file [types.h] [open_dir_2 100]
file [New0002.c] [open_dir_2 100]
file [simple.out] [open_dir_2 100]
file [server.c] [open_dir_2 100]
dir [learn] [open_dir_2 96]
file [simple_ffmpeg_player.c] [open_dir_2 100]
file [client] [open_dir_2 100]
dir [abc] [open_dir_2 96]
file [yasm-1.3.0.tar.gz] [open_dir_2 100]
file [output.yuv] [open_dir_2 100]
file [mySDLFirst.out] [open_dir_2 100]
-++--+++--[main 118]
root@ubuntu:/share#
- linux opendir和readdir的使用
- linux opendir readdir closedir 的使用
- linux opendir readdir closedir 的使用
- Linux下的opendir()和readdir()
- linux中DIR、dirent、opendir()、readdir()、closedir()函数的使用
- linux 下 opendir 和 readdir 函数的应用
- linux opendir 和 readdir , closedir用法
- linux opendir 和 readdir , closedir用法
- opendir()和readdir()的具体用法
- opendir()和readdir()的具体用法
- Linux chmod opendir readdir closedir
- linux 下opendir readdir 在windows下的替代函数
- linux 下opendir readdir 在windows下的替代函数
- linux 下opendir readdir 在windows下的替代函数
- Linux C/C++ ----目录文件的操作(opendir,readdir,closedir)
- linux c下目录文件的操作(opendir,readdir,closedir)
- opendir,closedir,readdir ,telldir的·使用详解及例子
- Opendir readdir
- 算法导论 用拟阵求解任务调度问题
- Zookeeper实例原生API--删除节点接口权限
- java 反射机制
- Android点亮屏幕并解锁
- 键值对操作
- linux opendir和readdir的使用
- 自定义View实心圆和圆环
- Android设计与实现-卷1-JNI框架基础
- SVM 透彻理解与分析
- Vue.js 运行环境搭建详解(基于windows的手把手安装教学)及vue、node基础知识普及
- Android【官方教程】NDK 构建项目
- oracle day01
- 【DRP】ajax总结
- Myeclipse运行JMS的生产者时,报错:“java.lang.UnsupportedClassVersionError : Unsupported major.minor version 51”