我是刚刚学习C语言不久的新人,今天老师布置了一题关于文件应用的排序,检验下来在文件的2个子函数处有错,请各位大人看看,帮帮我 谢谢
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
double score[3];
double avg;
};
void input(struct student a[],int n);
void sort(struct student a[],int n);
void write(struct student a[],int n);
void read(struct student a[],int n);
void print(struct student a[],int n);
void main()
{
struct student stud[5];
input(stud,5);
printf("排序前\n");
print(stud,5);
sort(stud,5);
printf("\n排序后\n");
print(stud,5);
write(stud,5);
read(stud,5);
printf("\n从文件读出的记录为:\n");
print(stud,5);
}
//输入
void input(struct student a[],int n)
{
struct student stud[5];
int i,j;
double sum;
for(i=0;i<5;i++)
{
sum=0.0;
printf("请输入第%d个学号及姓名:",i+1);
scanf("%d%s",&a[i].num,a[i].name);
fflush(stdin);
for(j=0;j<3;j++)
{
printf("请输入第%d个学号的第%d门成绩:",i+1,j+1);
scanf("%lf",&a[i].score[j]);
sum+=a[i].score[j];
}
a[i].avg=sum/3.0;
}
}
//冒泡排序
void sort(struct student a[],int n)
{
struct student stud[5],tem;
int i,j;
for(i=3;i>=0;i--)
for(j=4;j>=4-i;j--)
if(a[j].avg<a[j-1].avg)
{
tem=a[j];
a[j]=a[j-1];
a[j-1]=tem;
}
}
//文件写入
void write(struct student a[],int n);
{
struct student stud[5];
FILE *fp;
int i;
fp=fopen("paixu","wb+");
if(fp==NULL)
exit(0);
for(i=0;i<5;i++)
fwiter(&stud[i],sizeof(struct student),1,fp);
fclose(fp);
}
//文件读出
void read(struct student a[],int n);
{
struct student stud[5],b[5];
FILE *fp;
int i;
fp=fopen("paixu","r+b");
if(fp==NULL)
exit(0);
for(i=0;i<5;i++)
fread(&b[i],sizeof(struct student),1,fp);
fclose(fp);
}
//输出
void print(struct student a[],int n)
{
struct student stud[5];
int i,j;
for(i=0;i<5;i++)
{
printf("%3d %5s ",a[i].num,a[i].name);
for(j=0;j<3;j++)
printf("%5.2f\t",a[i].score[j]);
printf("%5.2f\t",a[i].avg);
printf("\n");
}
}