代码
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
int fdSrc;
int fdDes;
char *readbuf = NULL;
if(argc !=3)
{
printf("pararm error\n");
exit(-1);
}
fdSrc = open(argv[1],O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readbuf = (char*)malloc(sizeof(char)*size + 8);
int n_read = read(fdSrc,readbuf,size);
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
int n_write = write(fdDes,readbuf,size);
if(fdDes != -1)
{
printf("copy %d byte ,context: %s \n",size,readbuf);
}
close(fdSrc);
close(fdDes);
free(readbuf);
return 0;
}
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d86061dceb89e294898458cb12f0bf32.png)