文件IO 目录操作
1. 打开目录 opendir()
#include <sys/types.h>
#include <dirent.h> //directory entry -- 目录项
DIR *opendir(const char *name);
返回值:
成功返回目录流指针,失败返回NULL;
参数:
name: 要打开的目录名
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
参数:
dirp: 目录流指针
返回值:
成功返回 0, 失败返回 -1;
2. 创建目录 mkdir()
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
返回值:
成功返回 0, 失败返回 -1
参数:
pathname: 要创建目录名
mode: 赋予权限
3. 读取目录 readdir()
readdir 一次只能随机读取一个目录项
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
返回值:
成功返回结构体指针, 失败返回 NULL;
参数:
dirp: 目录流指针
struct dirent{
......
char d_name[256]; /* 文件名 */
};
4. 关闭目录 closedir()
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
返回值:
成功返回 0, 失败返回 -1;
参数:
dirp: 目录流指针
5. 获取文件属性 – lstat
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/* 不能查链接文件除了文件名的其他信息
int stat(const char *pathname, struct stat *statbuf);
*/
//int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
返回值:
成功返回 0, 失败返回 -1;
参数:
pathname: 文件名
statbuf: 结构体指针
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode 文件类型和权限*/
nlink_t st_nlink; /* Number of hard links 硬链接数*/
uid_t st_uid; /* User ID of owner 用户ID*/
gid_t st_gid; /* Group ID of owner 组ID*/
dev_t st_rdev; /* Device ID (if special file) */
off_t st_size; /* Total size, in bytes 文件大小字节为单位*/
blksize_t st_blksize; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification 最后一次修改文件 时间*/
struct timespec st_ctim; /* Time of last status change */
#define st_atime st_atim.tv_sec /* Backward compatibility 向后兼容*/
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
//st_mode 相关
//获取文件类型 若 st_mode & s_IFMT等于对应文件类型宏,文件类型宏对应文件类型
S_IFMT 0170000 bit mask for the file type bit field
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
eg.
struct stat *sb;
lstat(pathname, &sb);
if ((sb.st_mode & S_IFMT) == S_IFREG) {//是普通文件
}
//若st_mode & 对应权限宏 == 宏本身,表示具有该权限
S_IRWXU 00700 owner has read, write, and execute permission
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission】
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others (not in group) have read, write, and
execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
eg.
if(sb.st_mode & S_IRWXU)//有用户读写执行权限
获取用户信息和组信息:
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);//根据用户id获取用户信息
struct passwd {
char *pw_name; /* username */
char *pw_passwd; /* user password */
uid_t pw_uid; /* user ID */
gid_t pw_gid; /* group ID */
char *pw_gecos; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
#include <sys/types.h>
#include <grp.h>
struct group *getgrgid(gid_t gid);//根据组id获取组信息
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
......
};