#include "stdio.h"
//#include "unistd.h"
int main(int argc, char *argv[])
{
FILE *src_fp , *des_fp;//定义指向源文件和目标文件的指针
int read_ret;
char read_buf[128]={0};
//char buf[]="hello linux";
if(argc<3)
{
printf("src ? des? \n ");
return -1;
}
src_fp=fopen(argv[1],"r");
if(src_fp==NULL)
{
printf("open src %s fail\n",argv[1]);
return -2;
}
des_fp=fopen(argv[2],"w");
if(des_fp==NULL)
{
printf("open des %s fail\n",argv[2]);
return -3;
}
while(1) //重点在这里
{
read_ret=fread(read_buf,1,128,src_fp); //定义了128个字节大小的文件流
printf("read_ret=%d ",read_ret);
fwrite(read_buf,1,read_ret,des_fp); //写到目标文件中
if(read_ret < 128)
{
printf("read file %s end \n",argv[1]);
break;
}
// printf("begin write\n");
// fwrite(read_buf,1,read_ret,des_fp);
}
fclose(src_fp);
fclose(des_fp);
return 0;
}
运行后将a.c内容复制到b.c,结果如下: