以链表形式向文件输入数据,建立信息,然后想着写个输出函数看看是否输入
进去了,结果编译运行什么也没有显示。但编译通过没问题,不知道代码哪里
出来问题,想请教各位大神们帮忙看看代码,看看哪里有什么问题。时间有点
急!!在此谢谢大家啦!!!!
```#include
#include
#include
struct acc_message //定义结点的结构
{ char name[12];
int cardnum;
int password;
double acc_balance;
struct acc_message *next;
} ;
int main()
{
struct acc_message head; //创建空链表
head =(struct acc_message)malloc(sizeof (struct acc_message));
head -> next =NULL ;
struct acc_message *p, *q ;
q = head ;
FILE *fp; //建立原有银行客户信息文件
if ((fp = fopen("message.txt","wt+"))==NULL){
printf("Open this file error,press any key exit!/n ");
getchar();
exit(0);
}
int i=0;
while(i<5){
p =(struct acc_message*)malloc(sizeof (struct acc_message)); //创建新节点
if (p==NULL) break;
switch (i)
{ case 0:
fprintf (fp,"Jane 62221111 123456 8000/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 1:
fprintf (fp,"Alan 62221112 123456 500/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 2:
fprintf (fp,"Mary 62221113 123456 600000/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 3:
fprintf (fp,"Alice 62221114 123456 0/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
case 4:
fprintf (fp,"Kivin 62221115 123456 70000/n",p->name,p->cardnum,p->password,p->acc_balance);
break;
}
q -> next=p;
q=p;
i++;
}
q->next = NULL;
rewind (fp);
struct acc_message *r = head->next;
while (r!=NULL){
fscanf(fp,"%s %d %d %d/n",r->name,r->cardnum,r->password,r->acc_balance);
r=r->next;
}
fclose(fp);
}