作业目标:
用fgetc和fputc实现一个文件的拷贝,例如将a.c中的内容拷贝到b.c中
拷贝完毕后 可以用diff指令判断 两个文件是否相同: diff a.c b.c
代码如下:
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp1 = fopen("./user.txt","r");
if(NULL == fp1)
{
perror("fopen");
return -1;
}
FILE *fp2 = fopen("./usr.txt","w");
if(NULL == fp2)
{
perror("fopen");
return -1;
}
char cp = 0;
while(1)
{
cp = fgetc(fp1);
if(EOF == cp)
break;
fputc(cp,fp2);
}
printf("复制完成\n");
fclose(fp1);
fclose(fp2);
return 0;
}
终端执行结果:
ubuntu@ubuntu:IO$ gcc 03test.c
ubuntu@ubuntu:IO$ ./a.out
复制完成
ubuntu@ubuntu:IO$ diff ./user.txt ./usr.txt
ubuntu@ubuntu:IO$ cat 03test.c