【linux浅谈019】系统IO函数

系统IO函数

1. 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);
int creat(const char *pathname, mode_t mode);
int openat(int dirfd, const char *pathname, int flags);
int openat(int dirfd, const char *pathname, int flags, mode_t mode);
函数描述
open(打开文件(文件描述符) 方式和其他 权限)
必选项:
O_RDONLY
O_WRONLY
O_RDWR
可选项:
O_CREAT
文件权限: 本地有一个掩码
文件的实际权限:
给定的权限
本地掩码(取反)umask 可以自己改 如umask 0022
&
实际的文件权限
777
111111111
111111101
111111101
775

O_TRUNC 文件截断为0
O_EXCL 与 O_CREAT一起使用判断文件是否存在
O_APPEND

2. read

所需头文件
#include <unistd.h>
调用方式:
ssize_t read(int fd, void *buf, size_t count);
函数描述
open(打开文件(文件描述符) 缓冲区 缓冲区大小)
返回值描述:

  1. -1 读文件失败
  2. 0 文件读完了
  3. >0 读取的字节数

3. write

所需头文件
#include <unistd.h>
调用方式:
ssize_t write(int fd, const void *buf, size_t count);
函数描述
open(打开文件(文件描述符) 缓冲区 缓冲区大小)
返回值: 有符号

4. lseek :获取文件指针,值或者宏

#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
函数描述
lseek(文件描述符 指针偏移量 三个宏)
SEEK_SET 设定指针偏移位置
The file offset is set to offset bytes.
SEEK_CUR 当前位置加上一个偏移量
The file offset is set to its current location plus offset bytes.
SEEK_END 设置偏移到尾部
The file offset is set to the size of the file plus offset bytes.
返回值: int

5. close

所需头文件
#include <unistd.h>
调用方式:
int close(int fd);
函数描述
close(文件指针)
返回值:int

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值