#include <stdio.h>
#include <stdlib.h>
#define SIZE 2
struct student
{
int num;
char name[10];
float score;
} stu1[SIZE], stu2[SIZE], information_i;
void save ()
{
int i;
FILE *fp;
if ((fp=fopen("student_list.txt","wd"))==NULL)
{
printf("Cannot open tha file! \n");
exit(0);
}
for (i=0;i<SIZE;i++)
{
if(fwrite(&stu1[i],sizeof(struct student),1,fp) != 1)
{
printf("frile write error!");
}
fclose(fp);
}
}
void check()
{
int i;
FILE *fp;
if ((fp=fopen("student_list.txt","rd"))==NULL)
{
printf("Cannot open tha file! \n");
exit(0);
}
for(i=0;i<SIZE;i++)
{
fread(&stu2[i],sizeof(struct student),1,fp);
printf("%d %s %4.2f\n", stu1[i].num, stu1[i].name, stu1[i].score);
}
fclose(fp);
}
void check_i()
{
int i = 1;
FILE *fp;
if (!(fp=fopen("student_list.txt","r")))
{
printf("Cannot open tha file! \n");
exit(0);
}
rewind(fp);
fseek(fp, i*sizeof(struct student), 0);
fread(&information_i, sizeof(struct student), 1, fp);
printf("%d %s %4.2f\n", information_i.num, information_i.name, information_i.score);
fclose(fp);
}
int main () {
int i;
printf("Please enter student information(school_number、name、score):\n");
for (i=0;i<SIZE;i++)
{
scanf("%d %s %f",&stu1[i].num,stu1[i].name,&stu1[i].score);
}
save();
printf("The file for student_list.txt:\n");
check();
printf("The information of second student:\n");
check_i();
return 0;
}