顺序表的持久存储(persistent storage),为了使顺序表中的数据能长期存储,可用文件形式来保存顺序表中的内容,代码如下:
#include<stdio.h>
#include<stdlib.h>
#define Max 100//名字的最大长度
typedef struct{
char name[Max];
int age;
}Person;
typedef struct{
Person Table[Max];
int last;
}List,*LinkList;
void save(List person){
FILE *fp;
if((fp=fopen("person.dat","wb"))==NULL){
printf("不能打开文件");
}
if(fwrite(&person,sizeof(List),1,fp)!=1){
printf("文件写入失败");
}
fclose(fp);
}
void main(){
Person p1={"yzb",20};
Person p2={"tom",21};
List L;
L.Table[0]=p1;
L.last+=1;
L.Table[1]=p2;
L.last+=1;
save(L);
}
制作不易,还请多多打赏,谢谢