|

楼主 |
发表于 2005-2-23 05:24:07
|
显示全部楼层
- #include<stdlib.h>
- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include"err_exit.h"
- main(int argc,char *argv[])
- {
- int fd,n;char a;
- char *buf[BUFSIZ];
- if(argc!=2){
- printf("error\n");
- exit(1);
- }
- if((fd=open(argv[1],O_RDWR|O_APPEND))==-1){
- err_exit("open failed\n");
- }
- printf("(w)rite or (r)ead?\n");
- scanf("%c",&a);
- while(1){
- if(a=='r'){
- if((n=lseek(fd,10,SEEK_SET))==-1){
- err_exit("lseek");
- }
- if((n=read(fd,buf,(BUFSIZ)))<0){
- err_exit("wirte failed\n");
- }
- printf("READ FROM %s:%s.",argv[1],buf);
- printf("TOTAL %d bytes\n",n);
- exit(0);
- }
- else if(a=='w'){
- if((n=lseek(fd,-10,SEEK_END))==-1){
- err_exit("lseek failed\n");
- }
- *buf="hello";
- if((n=write(fd,buf,sizeof(buf)))<0){
- err_exit("wirte failed\n");
- }
- printf("READ FROM %s:%s.",argv[1],buf);
- printf("TOTAL %d bytes\n",n);
- exit(0);
- }
- else{
- exit(1);
- }
- }
- }
复制代码
我建立了个temp.file文件,里面内容就是26个子母,执行read时正常
为什么执行write就是程序中的r时候会把文件变成个32795字节的文件阿!? |
|