从键盘录入10个学生的学号、姓名、数学成绩,将其中60分以上(含60分)的同学信息全部写入
D:\my_data.txt文件中;再从文件中读出成绩,显示到屏幕上。尽量使用fwrite
求各位用c语言解答一下
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- CSDN专家-sinJack 2021-12-23 19:39关注
#include <stdio.h> typedef struct student { char id[10]; char name[10]; int math; } STU; const int N = 10; int main(int argc, char *argv[]) { STU stu[N]; for (int i = 0; i < N; i++) { scanf("%s%s%d", stu[i].id, stu[i].name, &stu[i].math); } FILE *fp = fopen("D:\\my data.txt", "w"); if (fp == NULL) { printf("打开文件错误!\n"); return 1; } int count = 0; for (int i = 0; i < N; i++) { if (stu[i].math >= 60) { fprintf(fp, "%s %s %d\n", stu[i].id, stu[i].name, stu[i].math); count++; } } fclose(fp); STU stuRead[N]; fp = fopen("D:\\my data.txt", "r"); if (fp == NULL) { printf("打开文件错误!\n"); return 1; } for (int i = 0; i < count; i++) { fscanf(fp, "%s%s%d", stuRead[i].id, stuRead[i].name, &stuRead[i].math); printf("%s %s %d\n", stuRead[i].id, stuRead[i].name, stuRead[i].math); } fclose(fp); getchar(); return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报