【C语言程序设计第四版】第十二章 程序设计题 2

第二题

将实数写入文件:从键盘输入若干实数(以特殊数值-1结束),分别写到一个文本文件中。

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

int main(void){
    double number;
    
    FILE *fp;
    if ((fp = fopen("number.txt", "w")) == NULL) {
        printf("Open file error.
");
        exit(0);
    }
    
    scanf("%lf", &number);
    
    while (number != -1 ) {
        fprintf(fp, "%lf ", number);
        scanf("%lf", &number);
    }
    
    if (fclose(fp)) {
        printf("Can not close the file!
");
        exit(0);
    }
    
    return 0;
    
}
原文地址:https://www.cnblogs.com/sidianok/p/15351899.html