C/perpect C
15장 파일처리 연습문제
옥식
2022. 1. 12. 02:41
01. 파일에서 자료를 읽어 학생들의 점수의 합을 구하여 출력
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int id;
char name[30];
double score[3];
double total;
}Student;
int main(void)
{
char fname[30];
FILE* f;
scanf("%s", fname);
f = fopen(fname, "r");
if (f == NULL)
{
printf("cannot open file");
exit(1);
}
Student m[3];
int i;
for (i = 0; i < 3; i++)
{
fscanf(f, "%d %s %lf %lf %lf", &m[i].id, &m[i].name, &m[i].score[0], &m[i].score[1], &m[i].score[2]);
m[i].total = m[i].score[0] + m[i].score[1] + m[i].score[2];
}
for (i = 0; i < 3; i++)
{
printf("%d %s %lf %lf %lf %lf\n", m[i].id, m[i].name, m[i].score[0], m[i].score[1], m[i].score[2], m[i].total);
}
return 0;
}