c语言问题,求大家帮助

 

编程有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学号,姓名,三门课成绩),计算出总成绩,将原有的数据和计算出的总分数存放在磁盘文件stu.txt中。

 

#include <stdio.h>
#include<stdlib.h>
#define SIZE 2
struct Student
{
    int num;
    char name[10];
    float score[3];
    float ave;

}stu[SIZE];


void save(struct Student stud[], int n)
{
    FILE* fp;
    int i;
    if ((fp = fopen("stu.txt", "wb")) == NULL)
    {
        printf("不能打开");
        exit(0);

    }

    for (i = 0; i < n; i++)
    {
        if (fwrite(&stud[i], sizeof(struct Student), 1, fp) != 1)
        {
            printf("NO !");
        }

    }
    fclose(fp);

}


void input(struct Student stu[])
{
    int i;
    printf("please input data of student:\n");
    for (i = 0; i < SIZE; i++)
    {
        scanf_s("%d %s %f %f %f", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
        stu[i].ave = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3;
    }


}

void print(struct Student stud[], int n)
{
    FILE* out;
    int i;
    if ((out = fopen("stu.txt", "rb")) == NULL)
    {
        printf("不能打开");
        exit(0);

    }
    for (i = 0; i < n; i++)
    {
        fread(&stud[i], sizeof(struct Student), 1, out);
        printf("%d %s %.2f %.2f %.2f %.2f\n", stud[i].num, stud[i].name, stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].ave);
    }

}


int main()
{
    void input(struct Student stu[]);
    int i;
    struct Student stu[SIZE], * p = stu;
    input(p);
    save(p, SIZE);
    print(p, SIZE);
    return 0;

}
 

 这个报错是为什么。

改好了

img


#include <stdio.h>
#include<stdlib.h>
#define SIZE 2
struct Student
{
    int num;
    char name[10];
    float score[3];
    float ave;

}stu[SIZE];


void save(struct Student stud[], int n)
{
    FILE* fp;
    int i;
    if ((fp = fopen("stu.txt", "wb")) == NULL)
    {
        printf("不能打开");
        exit(0);

    }

    for (i = 0; i < n; i++)
    {
        if (fwrite(&stud[i], sizeof(struct Student), 1, fp) != 1)
        {
            printf("NO !");
        }

    }
    fclose(fp);

}


void input(struct Student stu[])
{
    int i;
    printf("please input data of student:\n");
    for (i = 0; i < SIZE; i++)
    {
        scanf("%d %s %f %f %f", &stu[i].num, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
        stu[i].ave = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3;
    }


}

void print(struct Student stud[], int n)
{
    FILE* out;
    int i;
    if ((out = fopen("stu.txt", "rb")) == NULL)
    {
        printf("不能打开");
        exit(0);

    }
    for (i = 0; i < n; i++)
    {
        fread(&stud[i], sizeof(struct Student), 1, out);
        printf("%d %s %.2f %.2f %.2f %.2f\n", stud[i].num, stud[i].name, stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].ave);
    }

}


int main()
{
    void input(struct Student stu[]);
    int i;
    struct Student stu[SIZE], * p = stu;
    input(p);
    save(p, SIZE);
    print(p, SIZE);
    return 0;

}