拷贝函数

 fgetccp
****************************
#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
 FILE * fp,*fp1;
 char ch;
 if(argc!=3)
{
 perror("argc error!");
 exit(0);
}
 if((fp=fopen(argv[1],"r"))==NULL)
{
 printf("open %s error",argv[1]);
 exit(0);
}

 if((fp1=fopen(argv[2],"w"))==NULL)
{
 printf("open %s error",argv[2]);
 exit(0);
}
 ch=fgetc(fp);
 while(ch!=EOF)
 {
  fputc(ch,fp1);
 ch=fgetc(fp);
 }
 if(!feof(fp))printf("it is error!");
 
 fclose(fp);
 fclose(fp1);

}
fgetscp
************************************
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define SIZE 512
int main(int argc,char *argv[])
{
 FILE *in,*out;
 char buffer[SIZE];
 int n_chars;
 char ch;
 if((in=fopen(argv[1],"r"))==NULL)
{
 perror("fopen 1 error!");
}
 if((out=fopen(argv[2],"w+"))==NULL)
{
 perror("fopen 1 error!");
}
 memset(buffer,0,SIZE);

while(fgets(buffer,SIZE,in)!=NULL)
{
 if(fputs(buffer,out)==EOF)
 {
 printf("it is error!");
 }
}
 
 fseek(out,0,SEEK_SET);
 printf("rrrrr");
 while((n_chars=fread(buffer,1,SIZE,out))>0)
 {   printf("ssssssss");
    if(fwrite(buffer,1,n_chars,stdout)!=n_chars)
        perror("fwrite stdout error!");
}
fclose(in);
fclose(out);
return 0;
}

freadcp
************************************
#include<stdio.h>
#include<stdlib.h>

#define SIZE 512

int main(int argc,char *argv[])
{
 FILE *fp,*fp1;
 char buf[SIZE];
 int n_chars;
 if(argc!=3)
{
 perror("input error!");
 exit(1);
}
 if((fp=fopen(argv[1],"r"))==NULL)
{
 perror("cann`t open bank.c!");
 exit(0);
}
 if((fp1=fopen(argv[2],"w"))==NULL)
{
 perror("cann`t open bankcp.c");
 exit(0);
}
 //fseek(fp,0,SEEK_SET);
 //fseek(fp1,0,SEEK_SET);
bzero(buf,0);
 while((n_chars=fread(buf,1,SIZE,fp))>0)
{
 if(fwrite(buf,1,n_chars,fp1)==-1)
{
 perror("it is wrong!");
}
}
if(ferror(fp))printf("error");
if(feof(fp)) printf("it is right!");
else printf("this is error!");

fclose(fp);
fclose(fp1);

}

你可能感兴趣的:(拷贝函数)