#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
struct Student
{
char name[20];
int age;
int score;
};
void Input(struct Student *arr,int len)
{
for(int i=0;i<len;i++)
{
scanf("%s%d%d",&arr[i].name,&arr[i].age,&arr[i].score);
}
}
void Show1(struct Student *arr,int len)
{
for(int i=0;i<len;i++)
{
printf("%s,%d,%d\n",arr[i].name,arr[i].age,arr[i].score);
}
}
void Save(struct Student *arr,int len)
{
char *path = "D:\\1.txt";
FILE *fw = fopen(path,"wb");
assert(fw != NULL);
for(int i=0;i<len;i++)
{
fwrite(&arr[i],sizeof(struct Student),1,fw);
}
fclose(fw);
}
void Show()
{
char *path = "D:\\1.txt";
FILE *fr = fopen(path,"rb");
assert(fr != NULL);
struct Student stu;
while(fread(&stu,sizeof(struct Student),1,fr) > 0)
{
printf("%s,%d,%d\n",stu.name,stu.age,stu.score);
}
fclose(fr);
}
int main()
{
struct Student arr[3];
Input(arr,sizeof(arr)/sizeof(arr[0]));
Save(arr,sizeof(arr)/sizeof(arr[0]));
printf("保存数据完成\n");
Show();
return 0;
}