1.在ubuntu 通过touch 创建一个.c文件
2.文件代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(int argc,char *argv[])//带参数的main函数
{
int fd_src = open(src_path,O_RDWR);
if(fd_src < 0)
{
printf("open %s fail\n",src_path);
return 0;
}
//打开复制后的文件
int fd_dst = open(dst_path,O_RDWR|O_CREAT);
if(fd_dst < 0)
{
printf("open %s fail\n",dst_path);
return -1;
}
char buf[SIZE];
memset(buf,0,sizeof(buf));
int ret_rd,ret_wr;
int i=0;//复制的次数
//循环读取数据
while(1)
{
//ret_rd = read(fd_src,buf,1024);
ret_rd = read(fd_src,buf,SIZE);
ret_wr = write(fd_dst,buf,ret_rd);
printf("read:%d write:%d count:%d\n",ret_rd,ret_wr,++i);
//判断结束读取的条件
//if(ret_rd < 1024)
if(ret_rd < SIZE)
{
break;
}
}
//关闭文件
close(fd_src);
close(fd_dst);
return 0;
}
2.gcc xx.c -o xx 编译文件