Skip to content
On this page

open(2)


标签:linux/api  

函数原型

打开或者创建一个文件。

c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

参数

flags:

  • O_RDONLY 只读 , O_WRONLY 只写 , O_RDWR 读写(三者必有一)
  • O_APPEND 追加, O_CREAT 创建, O_TRUNC 截断为 0(0个或多个)
  • 还有其他。。。

这些宏可以通过 | 来一起使用,它是通过位图 #TBD/QS 实现的。

对应标准 I/O:

  • r --> O_RDONLY
  • r+ --> O_RDWR
  • w --> O_WRONLY | O_TRUNC | O_CREAT
  • w+ --> O_RDWR | O_TRUNC | O_CREAT
  • a --> O_WRONLY | O_APPEND | O_CREAT

mode: 当 flags 中出现 O_CREAT 时,这个参数必须使用。

返回

其他

  • fopen():标准IO打开文件,返回的是标准流

Last updated: