这个程序非常简单,但是在写程序时要注意,在实现复制的过程中,无论是复制的文件还是被复制的文件都应该是打开状态,复制完成后再分别关闭。
1. int fgetc(FILE * stream)
从 stream 所指的文件中读取一个字符,函数返回读取到的字符。
2. int fputc (int c, File *fp)
将字符 c 写入到 fp 所指向的文件中,正常情况下函数返回字符 c 的 ASCII 值。
/*该程序实现文件的复制*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * in,* out;
char ch,infile[50],outfile[50];
printf("Enter the infile name : \n");
scanf("%s",infile); //输入源文件名
printf("Enter the outfile name : \n");
scanf("%s",outfile); //输入目标文件名
if((in=fopen(infile,"r"))==NULL){ //源文件打开失败
printf("cannot open infile \n");
exit(0);
}
if((out=fopen(outfile,"w"))==NULL){ //目标文件打开失败
printf("cannot open outfile \n");
exit(0);
}
ch=fgetc(in); //从源文件中读取一个字符
while(ch!=EOF){ //未读到文件尾循环
fputc(ch,out); //将字符ch写入到out所指向的文件中
ch=fgetc(in); //从in所指向的文件中读取下一个字符
}
fclose(in); //关闭源文件
fclose(out); //关闭目标文件
return 0;
}


