系统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 读文件失败
- 0 文件读完了
- >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