- 论坛徽章:
- 0
|
- #include<stdio.h>
- #include<sys/types.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<sys/stat.h>
- #define readmsg 200
- int main(int argc,char *argv[])
- {
- int srcfd,aimfd,length;
- int readlength=0;
- int location;
- char buf[readmsg];
- if(argc!=3)
- {
- printf("Less of parament!\n");
- printf("Please input the command as follow form!\n");
- printf("ProgramName+SourceFileName+AimFileName.\n");
- exit(0);
- }
- if((srcfd=open(argv[1],O_RDONLY))==-1)
- {
- printf("Fail to open SourceFile!\n");
- exit(1);
- }
- if((aimfd=open(argv[2],O_RDWR|O_CREAT,0333))==-1)
- {
- printf("Fail to open AimFile!\n");
- exit(1);
- }
- length=lseek(srcfd,0,SEEK_END);
- printf("The lenth of SourceFile is %d bytes!\n",length);
- location=lseek(srcfd,0,SEEK_SET);
- while(length>readmsg)
- {
- if(readlength=(read(srcfd,buf,readmsg))==-1)
- {
- printf("Read SourceFile failure!\n");
- exit(1);
- }
- if((write(aimfd,buf,readmsg))==-1)
- {
- printf("Write to AimFile failure!\n");
- exit(1);
- }
- // location=lseek(srcfd,readmsg,SEEK_CUR);
- length=length-readmsg;
- }
- printf("Location is %d from the head of the file!\n");
- printf("Now the VAR length is %d!\n",length);
- read(srcfd,buf,length);
- printf("The messages read from the source file are\n%s\n",buf);
- write(aimfd,buf,length);
- close(srcfd);
- close(aimfd);
- exit(0);
- }
复制代码 |
|