1. 以二进制方式读写结构体

struct Student
{
	string name;
	string sex;
	int age;
}

void write(string filePath, const struct Student* stu, int n)
{
	FILE *fp;
	int i;
	if((fp=fopen(filePath,"wb"))==NULL)
	{
		printf("cant open the file");
		return;
	}
	for(i=0;i<n;i++)
	{
		if(fwrite(&stu[i],sizeof(struct Student),1,fp)!=1)
		printf("file write error\n");
	}
	fclose(fp);
}

void read(string filePath, const struct Student* stu, int n)
{
	FILE *fp;
	int i;
	if((fp=fopen(filePath,"rb"))==NULL)
	{
		printf("cant open the file");
		return;
	}
	for(i=0;i<n;i++)
	{
		if(fread(&stu[i],sizeof(struct Student),1,fp)!=1)
		printf("file read error\n");
	}
	fclose(fp);
} 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
猜你喜欢
  • 2021-09-09
  • 2021-08-19
  • 2021-11-18
  • 2022-02-04
  • 2021-07-21
相关资源
相似解决方案