目錄操作
linux 下一切皆文件
1、創建目錄
使用mkdr創建目錄
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
參數:
- pathname 待創建目錄路徑
- mode 創建目錄權限
返回值:成功返回0;出錯返回-1;
2、刪除目錄
使用rmdir刪除目錄
#include <unistd.h>
int rmdir(const char *pathname);
參數:
- pathname 要刪除的目錄,必須為空目錄
返回值:成功返回0;失敗返回-1;
3、打開目錄
使用opendir()打開目錄
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
參數:
- name 要打開的目錄名稱
返回值:
- 成功返回指向該目錄的指針;失敗返回NULL;
使用fdopendir()打開目錄****************
#include <sys/types.h>
#include <dirent.h>
DIR *fdopendir(int fd);
參數:
- fd *******************************
返回值:
- 成功返回指向該目錄的指針;失敗返回NULL;
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;