/*
根据cp命令的格式要求,设计一个类cp的功能程序,要求使用标准IO的调用函数,分3种形式实现,字符,行,块。并注意函数功能的分层
*/
#include<stdio.h>
#include <unistd.h>
/*fgetc fputc 一个字符*/
int cpchar(char *src, char *des)
{
FILE *fpsrc,*fpdes;
char ch;
fpsrc = fopen(src, "r");
if(fpsrc==NULL)
{
printf("open src fail\n");
return -1;
}
fpdes = fopen(des, "w");
if(fpdes==NULL)
{
printf("open des fail\n");
return -1;
}
while(1)
{
if((ch=fgetc(fpsrc))!=EOF)
{
fputc(ch,fpdes);
printf("%c",ch);
}
else
{
break;
}
}
fclose(fpsrc);
fclose(fpdes);
return 0;
}
/*fgets fputs 一行*/
int cpline(char *src, char *des)
{
FILE *fpsrc,*fpdes;
char buf[1024];
fpsrc = fopen(src, "r");
if(fpsrc==NULL)
{
printf("open src fail\n");
return -1;
}
fpdes = fopen(des, "w");
if(fpdes==NULL)
{
printf("open des fail\n");
return -1;
}
while(1)
{
if(fgets(buf,1024,fpsrc)!=NULL)
{
fputs(buf,fpdes);
printf("%s",buf);
}
else
{
break;
}
}
fclose(fpsrc);
fclose(fpdes);
return 0;
}
/*fread fwrite 一块*/
int cpblk(char *src, char *des)
{
FILE *fpsrc,*fpdes;
char buf[1024];
int num;
fpsrc = fopen(src, "r");
if(fpsrc==NULL)
{
printf("open src fail\n");
return -1;
}
fpdes = fopen(des, "w");
if(fpdes==NULL)
{
printf("open des fail\n");
return -1;
}
while(1)
{
if((num=fread(buf,sizeof(char),1024,fpsrc))>0)
{
fwrite(buf,sizeof(char),num,fpdes);
printf("%d ",num);
}
else
{
break;
}
}
fclose(fpsrc);
fclose(fpdes);
return 0;
}