#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

struct  Student
{
   
	char name[20];//姓名
	int age;//年龄
	int score;//分数
};

//从键盘读取几个学生信息,arr:数组名,len:数组长度
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");
	//Show1(arr,sizeof(arr)/sizeof(arr[0]));
	Show();

	return 0;
}