1. 程式人生 > 其它 >明解c語言 13-4

明解c語言 13-4

1、

#include <stdio.h>

int main(void)
{
    FILE *fp;
    char name[128];
    double height, weight;
    
    
    if((fp = fopen("a.txt", "w")) == NULL)
        printf("\aFile open failed.\n");
    else
    {
        int i;
        for(i = 0; i < 6; i++)
        {
            printf("NO%d.name = 
", i + 1); scanf("%s", name); printf("NO%d.height = ", i + 1); scanf("%lf", &height); printf("NO%d.weight = ", i + 1); scanf("%lf", &weight); fprintf(fp, "%s %.2f %.2f\n", name, height, weight); } fclose(fp); } return 0; }

2、

#include <stdio.h>

int
main(void) { FILE *fp; typedef struct{ char name[128]; double height; double weight; }Type1; int i; Type1 x[6]; if((fp = fopen("a.txt", "w")) == NULL) printf("\aFile open failed.\n"); else { for(i = 0; i < 6; i++) { printf(
"x[%d].name = ", i); scanf("%s", x[i].name); printf("x[%d].height = ", i); scanf("%lf", &x[i].height); printf("x[%d].weight = ", i); scanf("%lf", &x[i].weight); fprintf(fp, "%s %.2f %.2f\n", x[i].name, x[i].height, x[i].weight); } fclose(fp); } return; }