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

2021/9/27 14:10:53

本文主要是介绍【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;
}

 



这篇关于【C语言程序设计第四版】例12-3代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程