c语言 13-4
1、
#include <stdio.h> #define NUMBER 6 typedef struct{ char name[128]; double height; double weight; }Type1; int main(void) { FILE *fp; fp = fopen("xxx.txt", "w"); if(fp == NULL) printf("\afile open failed.\n"); else { Type1 x[NUMBER]; int i; for(i = 0; i < NUMBER; i++) { printf("NO.%d name: ", i + 1); scanf("%s", x[i].name); printf("NO.%d height: ", i + 1); scanf("%lf", &x[i].height); printf("NO.%d weight: ", i + 1); scanf("%lf", &x[i].weight); fp = fopen("xxx.txt", "a"); fprintf(fp, "%s %8.2f %8.2f\n", x[i].name, x[i].height, x[i].weight); fclose(fp); } } return 0; }
2、
#include <stdio.h> #define NUMBER 6 int main(void) { FILE *fp; char name[128]; double height, weight; fp = fopen("xxx.txt", "w"); int i = NUMBER; while(i > 0 && scanf("%s%lf%lf", name, &height, &weight)) { fprintf(fp, "%-8s%8.2f%8.2f\n", name, height, weight); i--; } return 0; }
3、
#include <stdio.h> #define NUMBER 6 typedef struct{ char name[128]; double height; double weight; }Type1; int main(void) { FILE *fp; fp = fopen("xxx.txt", "w"); Type1 x[NUMBER]; int i; for(i = 0; i < NUMBER; i++) { printf("NO.%d name: ", i + 1); scanf("%s", x[i].name); printf("NO.%d height: ", i + 1); scanf("%lf", &x[i].height); printf("NO.%d.weight: ", i + 1); scanf("%lf", &x[i].weight); fprintf(fp, "%-8s%8.2f%8.2f\n", x[i].name, x[i].height, x[i].weight); } fclose(fp); return 0; }
4、
#include <stdio.h> #define NUMBER 3 int main(void) { FILE *fp; fp = fopen("xxx.txt", "w"); char name[128]; double height; double weight; int i; for(i = 0; i < NUMBER; i++) { printf("person%d.name: ", i + 1); scanf("%s", name); printf("person%d.height: ", i + 1); scanf("%lf", &height); printf("person%d.weight: ", i + 1); scanf("%lf", &weight); fprintf(fp, "%-8s%8.2f%8.2f\n", name, height, weight); } fclose(fp); return 0; }
5、
#include <stdio.h> int main(void) { FILE *fp; fp = fopen("xxx.txt", "w"); char name[128]; double height, weight; while(scanf("%s%lf%lf", name, &height, &weight) == 3) { fprintf(fp, "%-8s%8.2f%8.2f\n", name, height, weight); } fclose(fp); return 0; }
6、
#include <stdio.h> int main(void) { FILE *fp; fp = fopen("xxx.txt", "w"); char name[128]; double height, weight; int i = 2; while(i-- > 0) { scanf("%s%lf%lf", name, &height, &weight); fprintf(fp, "%-8s%8.2f%8.2f\n", name, height, weight); } fclose(fp); return 0; }