目录IO
1.mkdir 创建目录文件
mkdir(const char *pathname, mode_t mode);
(文件路径,文件的权限)
成功返回 0
失败返回 -1
r: 目录中是否能够查看文件
w: 目录中是否能够新建文件
x: 目录是否能够进入
2.rmdir 删除空目录文件
rmdir(const char *pathname);
(文件路径)
成功返回0
失败返回-1
3.1opendir 打开目录获得目录流指针
DIR *dp = NULL;
dp = opendir("m"); 【目录必须是一个文件夹】,
成功返回 目录流指针
失败返回 NULL
3.2closedir 关闭目录流指针
closedir(dp);
5.readdir 从目录流中读取下一个目录项的结构体信息
struct dirent *pp = NULL;
pp = readdir(dp); 【readdir必须用dirent 的结构体去接,因为读出来的是目录里的一个个文件,】
成功返回 包含目录项信息的空间首地址
失败返回 NULL
读到文件末尾返回 NULL
struct dirent {
ino_t d_ino; /* Inode number */
off_t d_off; /* Not an offset; see below */
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]; /* Null-terminated filename */
};
6.chdir 切换当前代码的工作路径
chdir(const char *path);
7.getcwd 获得当前目录的绝对路径
getcwd(tmpbuff, sizeof(tmpbuff));
8.access 检测调用函数的程序对文件是否拥有指定权限
access(文件路径, mode);
mode:
R_OK 检测是否拥有读权限
W_OK 检测是否拥有写权限
X_OK 检测是否拥有执行权限
F_OK 检测文件是否存在
有该权限返回 0
出错返回 -1
时间的获取
1. time 返回1970-1-1到现在的秒数(格林威治时间)
time_t = t;
time(&t); 【将此时的时间以秒为单位写入t中,此处应取t的地址】
成功返回秒数
失败返回-1
2. localtime 将秒数转换为本地时间
struct tm * tm = NULL; 【此时的结构体tm中,含有具体年月日,时分秒,用于接收】
tm = localtime(&t); 【括号中应指向t的地址】
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
成功返回结构体时间
失败返回NULL
3.mktime
mktime(tm);
将本地时间转换为秒数
文件属性和权限的获取:
1.stat 将pathname对应的文件信息放入statbuf中
struct star buf ;
int ret = 0
ret = stat(arvg[1],&buf);
(文件路径字符串的首地址,存放文件信息空间的首地址)
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 */
gid_t st_gid; /* Group ID of owner */
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 */
/* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */
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
};
成功返回0
失败返回-1
【注意:stat 调用S_IFLNK时 会直接得到该文件指向的文件中,此时需要用lstat,得到该文件】;
2.1 getpwuid 通过UID获得对应的用户信息
struct passwd *pwd = NULL; 【此处要接对应st_uid库中的一整个信息,要用结构体】
pwd = getpwuid(buf.st_uid); 【拿出buf里 的 st_uid, 库中一一对应,得到对应的用户信息】
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 */
};
成功返回 包含用户信息的结构体
失败返回 NULL
2.2.getgrgid 通过组ID获得组信息
struct group * grp = NULL;
grp = getgrgid(gid_t gid);
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* NULL-terminated array of pointers
to names of group members */
};
成功返回包含组信息的结构体
失败返回NULL
3.readlink 读取连接文件本身的内容
readlink(argv[1], tmpbuff, sizeof(tmpbuff));
(链接文件的路径,存放数据空间首地址,最大存放数据字节数)
成功返回读到字节个数
失败返回-1
软连接和硬链接:
1.软连接(符号链接)
通过文件名链接,所有能够看到的连接文件均为软连接文件
ln -s file.txt a.txt
2.硬链接
通过文件对应的inode节点链接
ln file.txt b.txt
【注意:后面的 a.txt 和 b.txt 不能存在】