linux文件操作篇打开和关闭文件

2.1 打开文件和关闭文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 头文件
int open(const char *pathname, int flags); 打开一个文件
int close(int fildes); 关闭一个文件

1.打开文件
int open(const char *pathname, int flags);
//const char *pathname 是要打开的文件路径
//int flag 是文件打开的标志 。 标志有 主标志 和 副标志 。
// 主标志是互斥的。三选一
// O_RDONLY    只读方式打开
// O_RDWR     读写方式打开
// O_WRONLY    只写方式打开
// 副标志可以多选
// O_APPEND    读写文件从文件末尾处追加
// O_TRUNC     若文件存在并可写,则用清空的方式打开文件
// O_CREAT     若文件不存在,则创建该文件
// O_EXCL      ??
​
// 如果用O_CREAT 方式创建不存在的文件, open则需要额外设置文件权限
int open(const char *pathname, int flags, mode_t mode);
//mode_t mode 用0755 或者其他权限写入即可 . 
//创建文件的另一个函数,用法同open
int creat(const char* pathname, mode_t mode);
2.关闭文件
// 在使用
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值