open()函数
1. 函数原型
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
参数解析
打开方式
- O_RDONLY(仅读)
- O_WRONLY(仅写)
- O_RDWR (读写)
- O_CREAT(当文件不存在时,创建该文件)
- O_NONBLOCK (以非阻塞的方式打开)
- O_TRUNC (将文件的长度截为0)
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int fd;
fd = open("./dict.txt",O_RDONLY); /*返回一个文件句柄*/
printf("fd = %d\n",fd);
close(fd);
return 0;
}