2. 文件 IO 基础
2.1 open、read、write、close
请看文章:文件,基础IO
2.2 lseek
对于每个打开的文件,系统都会记录它的续写位置偏移量,当调用 read 和 write 函数时,就会从当前偏移位置开始读写。
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
参数:
offset: 偏移量,以字节为单位
whence: 相对偏移的参考值,可以是 SEEK_SET
(文件头部),offset 只能为正;SEEK_CUR
(文件当前位置),offset 可以为正或负;SEEK_END
(文件末尾),offset 可为正或负
返回值:成功返回当前位置,失败返回 -1