求助将链表中数据写入文件,将文件中数据读取并建立链表的问题
struct room{
int floor;
int num;
int type;
char direction[2];
int price;
char ruzhu;
struct room *next;
};
struct room *writeroom(struct room *head)
{
FILE *p;
p=fopen("room","w");
fwrite(head,sizeof(struct room),1,p);
do
{
head=head->next;
fwrite(head,sizeof(struct room),1,p);
}while(head);
fclose(p);
}
struct room *readroom()
{
FILE *p;
struct room *head,*tail,*new1;
p=fopen("room","r");
new1=(struct room *)malloc(sizeof(struct room));
tail=(struct room *)malloc(sizeof(struct room));
head=new1;
while(fread(tail,sizeof(struct room),1,p))
{
new1->next=tail;
new1=tail;
tail=(struct room *)malloc(sizeof(struct room)); }
new1->next=NULL;
fclose(p);
return head;
}
这两个函数有问题吗,我写入读取再输出会有乱码输出