上嵌学习笔记

系统编程-------目录操作

2017-01-09  本文已影响6人  Hassan_chao

目录操作

linux 下一切皆文件

1、创建目录

使用mkdr创建目录

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

参数:

返回值:成功返回0;出错返回-1;

2、删除目录

使用rmdir删除目录

#include <unistd.h>
int rmdir(const char *pathname);

参数:

返回值:成功返回0;失败返回-1;

3、打开目录

使用opendir()打开目录

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);

参数:

返回值:

使用fdopendir()打开目录****************


#include <sys/types.h>
#include <dirent.h>
DIR *fdopendir(int fd);

参数:

返回值:

4、读取目录信息

使用readdir()读取目录信息*******************************

#include <dirent.h>
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
​```


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 */
};


参数:

- struct dirent  结构体,定义目录的各种信息

## 5、关闭目录

> 使用closedir()关闭目录

include <sys/types.h>

include <dirent.h>

int closedir(DIR *dirp);

参数:

- dirp      文件指针

返回值:

- 成功返回0;失败返回-1;
上一篇 下一篇

猜你喜欢

热点阅读