Replace
nbytes = lseek(fd1, 10, 0)
while((nbytes = read(fd1, buf, BUFSIZ))>0){
if(write(fd2, buf, nbytes) < 0){
fprintf(stderr, "не понимаю\n");
break;
}
}
with
while((nbytes = read(fd1, buf, BUFSIZ))>0){
/* first you write your 20 bytes */
if(write(fd2, buf, nbytes) < 0){
fprintf(stderr, "не понимаю\n");
break;
}
/* and then you move 10 bytes further */
if (lseek(fd1, 10, 0) < 0){
/* break if this is the eof */
break;
}
}
如果 BUFSIZ 为 20。
或者,最简单的是 BUFSIZ = 30 :
while((nbytes = read(fd1, buf, BUFSIZ))>0){
/* you only write the first 20 read bytes, or nbytes
if there are no much bytes in the buffer */
if(write(fd2, buf, nbytes < 20 ? nbytes : 20) < 0){
fprintf(stderr, "не понимаю\n");
break;
}
}