改编c语言小程序

来源:百度知道 编辑:UC知道 时间:2024/06/10 23:32:03
键入5个学生的有关信息转存到磁盘文件上.学生信息包括姓名 学号 年龄 成绩.
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
main()
{struct student_type
{char name[10];
int num;
int age;
int score;
}stud[SIZE];
FILE *fp;
int i;
if((fp=fopen("stulist","wb"))==NULL)
{printf("can not open file.\n");exit(0);}
for(i=0;i<SIZE;i++)
{scanf("%s%d%d%d",stud[i].name,&stud[i].num,&stud[i].age,&stud[i].score);
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
{printf("file write error!\n");break;}
}
fclose(fp);
}

改编要求:在其他信息不变的基础上增加一门成绩

#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
main()
{struct student_type
{char name[10];
int num;
int age;
int score;
int score2
}stud[SIZE];
FILE *fp;
int i;
if((fp=fopen("stulist","wb"))==NULL)
{printf("can not open file.\n");exit(0);}
for(i=0;i<SIZE;i++)
{scanf("%s%d%d%d%d",stud[i].name,&stud[i].num,&stud[i].age,&stud[i].score,&stud[i].score2);
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
{printf("file write error!\n");break;}
}
fclose(fp);
}

#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
main()
{struct student_type
{char name[10];
int num;
int age;
int score[2];
}stud[SIZE];
FILE *fp;
int i;
if((fp=fopen("stulist","wb"))==NULL)
{printf("can not open file.\n&