第一种:fgetc fputc
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *src,*dst;
int buf;
if(argc < 3){
fprintf(stderr,"Usage:%s <src_file> <dst_file>\n",argv[0]);
exit(1);
}
src=fopen(argv[1],"r");
if(src == NULL){
perror("fopen():");
exit(1);
}
dst=fopen(argv[2],"w");
if(dst == NULL){
fclose(src);
perror("fopen():");
exit(1);
}
while((buf = fgetc(src)) != EOF)
{
fputc(buf,dst);
}
fclose(dst);
fclose(src);
exit(0);
}
第二种:fgets fputs
#include<stdio.h>
#include<stdlib.h>
#define BUFSIZE 1024
int main(int argc,char *argv[])
{
FILE *src,*dst;
char buf[BUFSIZE];
if(argc < 3){
fprintf(stderr,"Usage:%s <src_file> <dst_file>\n",argv[0]);
exit(1);
}
src=fopen(argv[1],"r");
if(src == NULL){
perror("fopen():");
exit(1);
}
dst=fopen(argv[2],"w");
if(dst == NULL){
fclose(src);
perror("fopen():");
exit(1);
}
while((fgets(buf,BUFSIZE,src)) != NULL)
{
fputs(buf,dst);
}
fclose(dst);
fclose(src);
exit(0);
}
第三种:fread fwrite
#include<stdio.h>
#include<stdlib.h>
#define BUFSIZE 1024
int main(int argc,char *argv[])
{
FILE *src,*dst;
char buf[BUFSIZE];
int n;
if(argc < 3){
fprintf(stderr,"Usage:%s <src_file> <dst_file>\n",argv[0]);
exit(1);
}
src=fopen(argv[1],"r");
if(src == NULL){
perror("fopen():");
exit(1);
}
dst=fopen(argv[2],"w");
if(dst == NULL){
fclose(src);
perror("fopen():");
exit(1);
}
while((n=fread(buf,1,BUFSIZE,src)) > 0)
{
fwrite(buf,1,n,dst);
}
fclose(dst);
fclose(src);
exit(0);
}
转载于:https://blog.51cto.com/zhangmujin/1349926