• 【C语言程序设计第四版】练习12-6


    实数取整写入文件

    #include <stdio.h>
    #include <stdlib.h>
    #define MAXN 300
    
    
    int main(void){
        
        int i,j;
        FILE *fp, *fp1;
        double nums[MAXN];
        
        
        
        if ((fp=fopen("f1.txt", "r")) == NULL) {
            printf("File open errpr!
    ");
            exit(0);
        }
        
        if ((fp1=fopen("f2.txt", "w")) == NULL) {
            printf("File open errpr!
    ");
            exit(0);
        }
        
        
        i = 0;
        while (!feof(fp)) {
            fscanf(fp, "%lf", &nums[i]);
            i++;
        }
    
        for (j=0; j<i-1; j++) {
            fprintf(fp1, "%.0f ", nums[j]);
        }
        
        
        if ((fclose(fp))) {
            printf("Can not close the file!
    ");
            exit(0);
        }
        
        if ((fclose(fp1))) {
            printf("Can not close the file!
    ");
            exit(0);
        }
    
        return 0;
    }

    改进版本,不需要定义数组

    #include <stdio.h>
    #include <stdlib.h>
    #define MAXN 300
    
    
    int main(void){
        
        int i,j;
        FILE *fp, *fp1;
        double nums;
        
        
        
        if ((fp=fopen("f1.txt", "r")) == NULL) {
            printf("File open errpr!
    ");
            exit(0);
        }
        
        if ((fp1=fopen("f2.txt", "w")) == NULL) {
            printf("File open errpr!
    ");
            exit(0);
        }
        
        
        fscanf(fp, "%lf", &nums);
        while (!feof(fp)) {
            fprintf(fp1, "%.0f ", nums);
            fscanf(fp, "%lf", &nums);
        }
        
        if ((fclose(fp))) {
            printf("Can not close the file!
    ");
            exit(0);
        }
        
        if ((fclose(fp1))) {
            printf("Can not close the file!
    ");
            exit(0);
        }
    
        return 0;
    }
  • 相关阅读:
    Teacher Bo HDU 5762(暴力)
    The Unique MST POJ1679(次小生成树)
    Sqrt Bo hdu 5752
    Borg Maze POJ 3026(BFS+最小生成树)
    Highways POJ 1751(最小生成树)
    hdu---2050---折线分割平面
    POj---1469---Courses
    poj---2349---Arctic Network
    poj-2528-Mayor's posters
    POJ---3468---A Simple Problem with Integers
  • 原文地址:https://www.cnblogs.com/sidianok/p/15343767.html
Copyright © 2020-2023  润新知