struct stu
{long num;
char name[10];
int score;
struct stu *next;
};
#define SIZE (&(((struct stu *)NULL)->next))
int save(struct stu *l, char *file)
{
FILE *fp;
fp = fopen(file, "wb");
if(fp == NULL) return 0;
while(l)
{
fwrite(l, SIZE, 1, fp);
}
fclose(fp);
return 0;
}
struct stu *load(char *file)
{
FILE *fp;
struct stu *h=NULL, *p,*t;
fp = fopen(file, "wb");
if(fp == NULL) return NULL;
while(l)
{
t = (struct stu *)malloc(sizeof(struct stu));
fread(t, SIZE, 1, fp);
t->next=NULL;
if(feof(fp))
{
free(t);
break;
}
if(h==NULL) h = p = t;
else
{
p->next= t;
p=t;
}
}
fclose(fp);
return h;
}
以上是不带头结点链表的读写。
带头结点的, 只需要稍做改动即可。