《UNIX环境高级编程》笔记--read函数,write函数,lseek函数
1.read函数调用read函数从文件去读数据,函数定义如下:
#include <fcntl.h>#include <stdio.h>int main(void){ int fd,byteNum,result; char wbuf[10] = "123456789"; char rbuf[10]; if((fd = open("./a.txt", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR))<0){ perror("open"); return -1; } if((byteNum = write(fd, wbuf, 10))<0){ perror("write"); return -1; } if((result = lseek(fd, 40960, SEEK_END))<0){ perror("lseek"); return -1; } if((byteNum = write(fd, wbuf, 10)) < 0){ perror("write"); return -1; } close(fd); return 0;}运行结果:-rw------- 1 root root 40980 2013-09-09 15:03 a.txt使用od命令查看文件内容:root@virtual-machine:~# od -c a.txt