C语言库函数---open

需要的头文件:
#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);
参数描述:
pathname:文件目录
flags:
三选一
O_RDONLY只读模式
O_WRONLY只写模式
O_RDWR读写模式
可选
O_APPEND每次写操作都写入文件的末尾
O_CREAT如果指定文件不存在,则创建这个文件
O_EXCL如果要创建的文件已存在,则返回-1,并且修改errno的值
O_TRUNC如果文件存在,并且以只写/读写方式打开,则清空文件全部内容(即将其长度截短为0)
O_NOCTTY如果路径名指向终端设备,不要把这个设备用作控制终端。
O_NONBLOCK如果路径名指向FIFO/块文件/字符文件,则把文件的打开和后继I/O
mode:文件权限,举例:fd_open=open(path,flags,0666)
相关函数
close(fd);
write(int fd,char * buf,size_t size);
read(int fd,char * buf,size_t size);
返回值
正确:文件描述符
错误:-1

发布了58 篇原创文章 · 获赞 6 · 访问量 6884

猜你喜欢

转载自blog.csdn.net/qq_23929673/article/details/95724915