- open/close函数
int open(const char *pathname, int flags); // 打开已存在文件,使用默认权限
int open(const char *pathname, int flags, mode_t mode); // 创建并打开新文件,并设置权限mode
// The argument flags must include one of the following access modes:
// O_RDONLY, O_WRONLY, or O_RDWR.
// O_APPEND:追加
// O_CREAT:创建
// O_EXCL:文件是否存在
// O_TRUNC:把文件截断成零,即文件清零
// O_NONBLOCK:非阻塞
// 创建文件时,指定文件访问权限,权限同时受umask影响,结论为:
// 文件权限 = mode & ~umask
int close(int fd);
- 头文件
#include<unistd.h>
#include<fcntl.h>
- 常见错误
1、打开文件不存在
2、以写方式打开只读文件(打开文件没有对应权限)
3、以只写方式打开目录