/**
 * Copyright (c) 2001,2002 Yoon, Hyunho <hhyoon@kldp.org>
 * http://mooo.org
 * ----------------------------------------------------------------------
 *
 * LICENSE
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_MAX        81
#define NUM_MAX        10
/* 구조체 정의 */
typedef struct student
{    char name[STR_MAX];
    int number;
    int kor;
    int eng;
    int math;
    int music;
    int computer;
    int total;
    float average;
} Student;
/* 함수 원형 선언 */
void input(Student *man, int num);
void sort(Student *man, int num);
void print(Student *man, int num);
int finput(Student *man);
int finputb(Student *man);
void fprintb(Student *man, int num);
/* 메인 함수 */
int main()
{    int num;
    Student man[NUM_MAX];
    num = finputb(man);
    print(man, num);
    sort(man, num);
    print(man, num);
    fprintb(man, num);
    return 0;
}/* 함수 input() */
void input(Student *man, int num)
{    int i;
    for (i = 0; i < num; i++) {
        printf("%d번째 학생 정보 입력해!\n", i + 1);
        printf("이름: ");
        flushall();
        fgets(man[i].name, STR_MAX, stdin);
        printf("학번: ");
        fscanf(stdin, "%d", &man[i].number);
        printf("국어: ");
        fscanf(stdin, "%d", &man[i].kor);
        printf("영어: ");
        fscanf(stdin, "%d", &man[i].eng);
        printf("수학: ");
        fscanf(stdin, "%d", &man[i].math);
        printf("음악: ");
        fscanf(stdin, "%d", &man[i].music);
        printf("바보: ");
        fscanf(stdin, "%d", &man[i].computer);
        man[i].total = man[i].kor + man[i].eng + man[i].math + man[i].music + man[i].computer;
        man[i].average = (float) man[i].total / 5;
    }
}/* 함수 sort() */
void sort(Student *man, int num)
{    int i, j;
    Student temp;
    for (i = 0; i < num - 1; i++) {
        for (j = 0; j < num - 1; j++) {
            if (man[j].total < man[j + 1].total) {
                temp = man[j];
                man[j] = man[j + 1];
                man[j + 1] = temp;
            }
        }
    }
}/* 함수 print() */
void print(Student *man, int num)
{    int i;
    for (i = 0; i < num; i++) {
        printf("%s\t", man[i].name);
        printf("%10d", man[i].number);
        printf("%5d", man[i].kor);
        printf("%5d", man[i].eng);
        printf("%5d", man[i].math);
        printf("%5d", man[i].music);
        printf("%5d", man[i].computer);
        printf("%7d", man[i].total);
        printf("%7.2f\n", man[i].average);
    }
    printf("\n");
}void fprintb(Student *man, int num)
{    FILE *fp;
    if ((fp = fopen("datab.txt", "wb")) == NULL) {
        printf("Error! Could not open the file..\n");
        exit(1);
    }
    fwrite(&num, sizeof(int), 1, fp);
    fwrite(man, sizeof(Student), num, fp);
    fclose(fp);
}/* 함수 finput() */
int finput(Student *man)
{    int i, num, ch;
    char *newline;
    FILE *fp;
    if ((fp = fopen("data.txt", "r")) == NULL) {
        printf("Cannot open the file..\n");
        exit(1);
    }
    fscanf(fp, "%d", &num);
    for (i = 0; i < num; i++) {
        ch = fgetc(fp);
        fgets(man[i].name, STR_MAX, fp);
        newline = strstr(man[i].name, "\n");
        *newline = 0;
        fscanf(fp, "%d", &man[i].number);
        fscanf(fp, "%d", &man[i].kor);
        fscanf(fp, "%d", &man[i].eng);
        fscanf(fp, "%d", &man[i].math);
        fscanf(fp, "%d", &man[i].music);
        fscanf(fp, "%d", &man[i].computer);
        man[i].total = man[i].kor + man[i].eng + man[i].math + man[i].music + man[i].computer;
        man[i].average = (float) man[i].total / 5;
    }
    fclose(fp);
    return num;
}/* 함수 finput() */
int finputb(Student *man)
{    int num;
    FILE *fp;
    if ((fp = fopen("datab.txt", "rb")) == NULL) {
        printf("Cannot open the file..\n");
        exit(1);
    }
    fread(&num, sizeof(int), 1, fp);
    fread(man, sizeof(Student), num, fp);
    fclose(fp);
    return num;
}
이 작은숲 문서의 출처는 위키노트의 위키노트/C 언어 예제/학생 정보 파일에 저장 문서입니다.