linux文件夹操作函数
一、opendir 函数
1、作用:打开一个目录
2、函数原型:
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
3、返回值:
(1)、DIR结构体指针,该结构是一个内部结构,保存所打开的目录的信息,作用于类似于FILE结构。
struct __dirstream
{
void *__fd; /* `描述符的struct hurd_fd'指针 */
char *__data; /* 目录块。 */
int __entry_data; /* 条目号 */
char *__ptr; /* 当前指向该块的指针。*/
int __entry_ptr; /* 条目号 */
size_t __allocation; /* 分配给块的空间。 */
size_t __size; /* 块中的总有效数据。 */
__libc_lock_define (, __lock) /* 此结构的互斥锁。 */
};
typedef struct __dirstream DIR;
(2)、函数出错,返回NULL
二、readdir 函数
1、作用:读目录
2、函数原型:
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
3、返回值:
成功时,readdir() 返回指向 dirent 结构的指针。(这个结构是静态分配的;不要试图去free它。)
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字符 */
/*
对于d_type:
DT_BLK:块设备
DT_CHR:字符设备
DT_DIR:目录
DT_LNK:软连接
DT_FIFO:管道
DT_REG:普通文件
DT_SOCK:套接字文件
DT_UNKNOWN:未知
*/
}
readdir函数为非线程安全函数;
三、readdir 函数
ref:
linux下C语言基本的目录操作编程函数简介_年纪青青的博客-CSDN博客
Linux目录操作—函数详解_Julinus的博客-CSDN博客
readdir
readdir(3) - Linux manual page
C++ readdir、readdir_r函数 - Delmory - 博客园
c - Segmentation fault checking strcmp - Stack Overflow