|
老师让编一个程序:如果启用添加标志打开一文件以便读写能否用lseek在任意位置读?能否在任意部分更新数据,写程序验证。这是一段程序,可是我看不懂,帮我解释一下好吗?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "ourhdr.h"
#define BUFFSIZE 1024
int
main(int argc, char **argv)
{
int fd,n;
char buf[BUFFSIZE];
if (argc!=2)
err_quit("usage: chapter3_6 testfile");
if ((fd=open(argv[1],O_RDWR | O_APPEND))<0)
err_sys("open error");
if (lseek(fd,6,SEEK_SET)<0)
err_sys("lseek error");
if ((n=read(fd,buf,BUFFSIZE))<0)
err_sys("read error");
if (write(STDOUT_FILENO,buf,n)<0)
err_sys("write to stdout error");
if (lseek(fd,6,SEEK_SET)<0)
err_sys("lseek2 error");
if (write(fd,buf,n)<0)
err_sys("write to testfile error");
close(fd);
exit(0);
} |
|