一、系统IO
1.write
#include
ssize_t write(int fd, const void *buf, size_t count);
参数:
fd:文件描述符
buf:从buf里面拿数据写出去
count:你想要写入到fd里面去的字节数
返回值:
成功:返回你实际写入的字节数
失败:-1

总结:
char buf[1024] = "abcdefg";
ret = write(fd,buf,10);
printf("ret=%d\n",ret);
当你的buffer里面只有7个字节,如果你想写入10个字节,实际返回值也是10;所以说count这个
参数的值,一定要按照你实际有效字节数来写;(注意它和read函数不一样)
2.lseek
#include
#include
off_t lseek(int fd, off_t offset, int whence);
参数:
fd:文件描述符
offset:偏移量
whence:基准坐标
SEEK_SET 文件头
The offset is set to offset bytes.
SEEK_CUR 当前位置
The offset is set to its current location plus offset bytes.
SEEK_END 文件尾
The offset is set to the size of the file plus offset bytes.
返回值:
成功:返回实际的偏移量(光标离文件头的的位置量)
失败:-1

练习1:
自己创建一个文件3.txt,随便在里面输入小于1024个字节
,通过lseek函数来计算出文件里面的字节数
printf("file size=