【C语言程序设计第四版】例12-3代码

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

int main(void){
    FILE *fp1, *fp2;
    
    char ch;
    
    if ((fp1 = fopen("f12-2.txt", "r")) == NULL) {
        printf("File open error!\n");
        exit(0);
    }
    
    if ((fp2 =fopen("f12-3.txt", "w")) == NULL) {
        printf("File open error!\n");
        exit(0);
    }
    
    while (!feof(fp1)) {
        ch = fgetc(fp1);
        if (ch!=EOF) {     // 只有当ch读取到EOF的时候,feof函数才返回1.
            fputc(ch, fp2);
        }
    }
    
    if (fclose(fp1)) {
        printf("Can not close the file!\n");
        exit(0);
    }
    if (fclose(fp2)) {
        printf("Can not close the file!\n");
        exit(0);
    }
    return 0;
}

 

posted @ 2021-09-27 14:00  就是想学习  阅读(55)  评论(0编辑  收藏  举报