open()
功能
打开文件
函数原型
int open(const char* pathname, int flags);
int open(const char* pathname, int flags, mode_t mode);
头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
参数
const char* pathname 指向要打开的文件的指针
int flags 打开文件的方式
O_WRONLY 只写
O_RDONLY 只读
O_RDWR 读写
O_CREAT 如果文件不存在则创建
O_APPEND 追加
返回值
返回文件描述符 失败则返回-1
read()
向文件内写入
函数原型
ssize_t read(int fd, void* buf, size_t count);
头文件
#include <unistd.h>
参数
int fd 要读取得文件的文件操作符
void* buf 读取文件存放的数组
size_t count 读取字符的个数
返回值
返回读取的字符数,如果返回值不等于count则出现错误
write
功能
从文件中读取
函数原型
ssize_t write(int fd, const void *buf, size_t count)
头文件
#include <unistd.h>
参数
int fd 文件操作符
const void *buf 要写的内容的字符串
size_t count 写的字符的个数
返回值
返回写入的字符数,失败返回-1;
lseek()
功能
改变文件指针位置
函数原型
off_t lseek(int fd, off_t offset, int whence)
头文件
#include <sys/types.h>
#include <unistd.h>
参数
int fd 文件操作符
off_t offset 想要的偏移量
int whence 选择文件的开头还是当前还是结尾
SEEK_SET 文件开头位置
SEEK_CUR 文件当前指针的位置
SEEK_END 文件结尾的位置
返回值
返回当前文件指针相对文件开头的偏移量
close()
功能
关闭文件
函数原型
int close(int fd)
头文件
#include <unistd.h>
参数
int fd 文件操作符
返回值
成功返回0失败返回-1