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

【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;
}

 

标签:ch,fp2,fclose,C语言,12,fp1,第四版,printf,exit
来源: https://www.cnblogs.com/sidianok/p/15342641.html