- 论坛徽章:
- 1
|
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
int fd;
char buf[20];
int size;
off_t location;
char * filename = "tt.dat";
if( (fd = open(filename,O_RDWR | O_CREAT | O_TRUNC)) == -1){
perror("open");
return 1;
}
size = write(fd,"Hello C Programming!\n",20);
if(size <0){
perror("write");
return 1;
}
printf("%d bytes write to %s successfully!\n",size,filename);
printf("the offset now is: %d\n",lseek(fd,0,SEEK_CUR));
if( (location = lseek(fd,5,SEEK_SET) > -1)){
printf("after lseeked %d .the offset now is: %d\n",location,lseek(fd,0,SEEK_CUR));
}
size = read(fd,buf,5);
if(size <0){
perror("read");
return 1;
}
printf("%d bytes(\"%s\") read from %s successfully!\n",size,buf,filename);
printf("the offset now is: %d\n",lseek(fd,0,SEEK_CUR));
return 0;
}
|
返回:
20 bytes write to tt.dat successfully!
the offset now is: 20
after lseeked 1 .the offset now is: 0
5 bytes(" C Pr") read from tt.dat successfully!
the offset now is: 10
问:after lseeked 1 .the offset now is: 0为什么不是预期要得到的:after lseeked 5 .the offset now is: 5
[ 本帖最后由 FinalBSD 于 2008-1-22 14:52 编辑 ] |
|