使用链表和fwrite对文件的读写发生错误
#include<stdio.h>#include<malloc.h>
#include<string.h>
#define NULL 0
struct booking
{
char num[4];
char TakeOffTime[5];
char ArriveTime[5];
char TakeOffCity[10];
char ArriveCity[10];
char TicketPrice[5];
struct booking *next;
};
//初始化节点
struct booking *node(struct booking *p){
p=(struct booking * )malloc(sizeof (struct booking));
FILE *fp;
printf("请输入航班序号:");
scanf("%s",&p->num);
printf("请输入航班起飞时间:");
scanf("%s",&p->TakeOffTime);
printf("请输入航班到达时间:");
scanf("%s",&p->ArriveTime);
printf("请输入航班起飞城市:");
scanf("%s",&p->TakeOffCity);
printf("请输入航班到达城市:");
scanf("%s",&p->ArriveCity);
printf("请输入航班票价:");
scanf("%s",&p->TicketPrice);
p->next=NULL;
if((fp=fopen("d:\\订票.txt","wb"))==NULL){
printf("can not open file\n");
return 0;
}
//fwrite(p,sizeof(struct booking),1,fp);
fwrite(p,sizeof(char),sizeof(struct booking),fp);
fclose(fp);
return p;
}
//录入
struct booking *creat(int n){
struct booking *head,*p1,*p2;
p1=node(p1);
head=NULL;
int i=0;
while(i<n){
if(i==0){
head=p1;
}
else{
p2=node(p2);
p1->next=p2;
p1=p2;
}
i++;
}
return head;
}
void display(struct booking *head)
{
struct booking *p;
p=head;
FILE *fp;
if((fp=fopen("d:\\订票.txt","rb"))==NULL){
printf("can not open file\n");
return;
}
while(p!=NULL){
//fread(p,sizeof(struct booking),1,fp);
fread(p,sizeof(char),sizeof(struct booking),fp);
p=p->next;
}
fclose(fp);
}
void main(){
struct booking *head;
head=creat(2);
display(head);
}