Tc中文件链表的存储与读取.
zkjun 2000-06-09 08:34:00 小弟向大家请教一个问题,我在程序中写了一个简单的单链表的结构存储到文件dai.dat中,现在要将文件上的内容读出来,我该在程序//.......//怎么写。希望能得到你到帮助,先谢谢大家了。
#include <stdio.h>
#include <alloc.h>
struct node
{char n;
struct node *next;
}zkj;
struct node *head,*r,*s,*p,*q;
struct node *creat()
{
char ch;
head=p=(struct node *)malloc(sizeof(struct node));
r=head;
ch=getchar();
while (ch!='$')
{s=(struct node *)malloc(sizeof(struct node));
s->n=ch;
r->next=s;
r=s;
ch=getchar();
}
r->next=NULL;
return head;
}
void saves(p)
struct node *p;
{FILE *fp;
fp=fopen("c:dai.dat","wb+");
while(p!=NULL)
{fwrite(p,sizeof(struct node),1,fp);
p=p->next;
}
fclose(fp);
}
main()
{FILE *fp;
head=creat();
saves(head);
}
*******************************************************************
*******************************************************************
*******************************************************************
#include <stdio.h>
#include <alloc.h>
struct node
{char n;
struct node *next;
}zkj;
struct node *p,*q;
main()
{FILE *fp;
fp=fopen("c:dai.dat","rb+");
while (!feof(fp))
{fread(//........//,sizeof(struct node),1,fp);
//.....
.....
//
printf("%c",p->n);
}
fclose(fp);
printf("\n");
}