加入CSDN是出于记录,希望有不足之处多加指正。
1、Linux 目录文件结构
2、目录检索相关API
opendir(打开目录文件)
头文件:
#include <sys/types.h>
#include <dirent.h>
函数原型:
DIR *opendir(const char *name);
参数分析:
name -->需要打开的目录的路径+名字
返回值:
成功:目录指针(类似open的返回值 / fopen)
失败:NULL
readdir(读取目录内容)
头文件:
#include <dirent.h>
原型:
struct dirent *readdir(DIR *dirp)
参数分析:
dirp --> 目录流指针
返回值:
成功:目录项指针
失败:NULL
struct dirent
{
ino_t d_ino; //文件索引号
off_t d_off; //目录项偏移量
unsigned short d_reclen; //该项目大小
unsigned char d_type; //文件类型
char d_name[256]; //文件名
};
closedir(关闭目录文件)
头文件:
#include <sys/types.h>
#include <dirent.h>
函数原型:
int closedir(DIR *dirp);
参数分析:
dirp --> 目录的流指针
返回值:
成功:返回 0
失败:返回 -1