C语言编程编译没问题但是在数据保存的时候总是显示内存溢出?
程序是一个酒店客房管理系统。
程序代码:/**********************************头文件***************************************/
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<malloc.h>
#define LEN sizeof(struct node)
#define len sizeof(struct GUEST)
/**********************************创建链表*******************************/
struct GUEST
{
long int ID;/**身份证**/
char name[8];/*姓名*/
char xb;/*性别*/
char telephone[11];/*电话*/
int check_inyear;
int check_inmonth;
int check_inday;
/*入住 日期*/
int check_outyear;
int check_outmonth;
int check_outday;
/*退房日期*/
char room[4];/*房间号*/
};
struct node
{
struct GUEST guest;
struct node *next;
};
/****************************显示主菜单*************************************/
void menu()
{
printf("******************酒店客房信息管理系统****************\n");
printf(" 1、登记顾客信息\n");
printf(" 2、修改顾客信息\n");
printf(" 3、寻找并显示客户信息\n");
printf(" 4、删除客户信息\n");
printf(" 5、客户退房\n");
printf(" 6、查看所有顾客信息\n");
printf(" 7、关键词查找(按入住日期查找)\n");
printf(" 8、退出并保存信息\n\n\n");
printf(" 请输入你选择的功能: ");
}
/*********************登记顾客信息**************/
struct node *regist(struct node *p)
{ static struct node *head;
struct node *p1;
head=p;
head->next=NULL;
return(head);
}
struct node *insert(struct node *head,struct node *p)
{
struct node *p1;
p1=head;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p;
p->next=NULL;
return(head);
}
/***************修改顾客信息******************/
void change(struct node *head,int num)
{
struct node *p0;
if(head==NULL)
printf("没人曾经入住过!\n");
else
{
p0=head;
while(p0->guest.ID!=num&&p0->next!=NULL)
p0=p0->next;
if(p0->next==NULL&&p0->guest.ID!=num)
printf("查无此人!\n");
else
{
printf("请输入身份证: ");
scanf("%ld",&p0->guest.ID);
fflush(stdin);
printf("请输入姓名: ");
scanf("%s",&p0->guest.name);
fflush(stdin);
printf("请输入性别:");
fflush(stdin);
scanf("%c",&p0->guest.xb);
fflush(stdin);
printf("请输入电话号码:");
scanf("%s",&p0->guest.telephone);
fflush(stdin);
printf("请输入房间号:");
scanf("%s",&p0->guest.room);
printf("信息修改完毕!\n ");
}
}
}
/******************寻找用户信息***************/
void seek(struct nide *head,int num)
{
struct node *p;
p=head;
while(p->guest.ID!=num&&p->next!=NULL)
p=p->next;
if(p->guest.ID==num)
{
printf("身份证: %ld\n",p->guest.ID);
printf("姓名: %s\n",p->guest.name);
printf("性别: %c\n",p->guest.xb);
printf("电话号码: %s\n",p->guest.telephone);
printf("入住时间: %d-%d-%d\n",p->guest.check_inyear,p->guest.check_inmonth,p->guest.check_inday);
if(p->guest.check_outyear==0)
printf("尚未退房\n");
else
printf("退房时间: %d-%d-%d\n",p->guest.check_outyear,p->guest.check_outmonth,p->guest.check_outday);
printf("房间号: %s\n",p->guest.room);
}
else
{
printf("error!\n");
exit(0);
}
}
/**************************删除顾客信息*********************/
struct node *shanchu(struct node *head,int num)
{
struct node *pre,*p;
if(head==NULL)
{
printf("没人曾经入住过!\n");
}
if(head->guest.ID==num)
{
head=head->next;
printf("信息删除成功!\n");
}
else
{
for(pre=head,p=pre->next;p!=NULL;pre=p,p=p->next)
if(p->guest.ID==num)
{
pre->next=p->next;
printf("信息删除成功!\n");
break;
}
}
return head;
}
/*********************退房**********************/
void check_out(struct node *head,int num)
{
int n;
struct node *p;
if(head==NULL)
printf("没人曾经入住过!\n");
else
{
p=head;
while(p->guest.ID!=num)
p=p->next;
puts("请输入退房日期: ");
scanf("%d%d%d",&p->guest.check_outyear,&p->guest.check_outmonth,&p->guest.check_outday);
puts("请输入入住天数: ");
scanf("%d",&n);
printf("房价为150元/天,需付现金%d元\n",150*n);
}
}
/*****************************显示所有用户信息******************/
void print(struct node *head)
{
struct node *p;
if(head==NULL)
printf("没人曾经入住过!\n");
else
{
p=head;
while(p!=NULL)
{
printf("身份证: %ld\n",p->guest.ID);
printf("姓名: %s\n",p->guest.name);
printf("性别: %c\n",p->guest.xb);
printf("电话号码: %s\n",p->guest.telephone);
printf("入住时间: %d-%d-%d\n",p->guest.check_inyear,p->guest.check_inmonth,p->guest.check_inday);
if(p->guest.check_outyear==0)
printf("尚未退房\n");
else
printf("退房时间: %d-%d-%d\n",p->guest.check_outyear,p->guest.check_outmonth,p->guest.check_outday);
printf("房间号: %s\n\n\n",p->guest.room);
p=p->next;
}
}
}
/*************关键字查找*************************/
void keyword(struct node *head,int a,int b,int c)
{
struct node *p;
if(head==NULL)
printf("没人曾经入住过!");
else
{
for(p=head;p!=NULL;p=p->next)
if(p->guest.check_inyear==a&&p->guest.check_inmonth==b&&p->guest.check_inday==c)
{
printf("身份证: %ld\n",p->guest.ID);
printf("姓名: %s\n",p->guest.name);
printf("性别: %c\n",p->guest.xb);
printf("电话号码: %s\n",p->guest.telephone);
printf("入住时间: %d-%d-%d\n",p->guest.check_inyear,p->guest.check_inmonth,p->guest.check_inday);
if(p->guest.check_outyear==0)
printf("尚未退房\n");
else
printf("退房时间: %d-%d-%d\n",p->guest.check_outyear,p->guest.check_outmonth,p->guest.check_outday);
printf("房间号: %s\n\n\n",p->guest.room);
}
else
printf("那天无人入住!\n");
}
}
/**********************************数据保存*********/
void save(struct node *head)
{
FILE *fp;
struct node *p=head;
if(fp=fopen("guest.dat","wb")==NULL)
{
printf("cannot open file!\n");
return;
}
while(p!=NULL)
{
fwrite(p,len,1,fp);
p=p->next;
}
fclose(fp);
}
/****************信息装载*********************/
struct node *load()
{
struct node *head=NULL;
struct node *p1,*p2;
FILE *fp;
if((fp=fopen("guest.dat","rb"))==NULL)
{
printf("打开文件出错\n");
exit(0);
}
while(!feof(fp))
{
if((p1=(struct node *)malloc(LEN))==NULL)
{
printf("内存申请出错\n");
fclose(fp);
exit(0);
}
if(head==NULL)
head=p2=p1;
else
{
p2->next=p1;
p2=p1;
}
}
fclose(fp);
return (head);
}
/*******************主函数******************/
main()
{
int i,a,b,c,d;
long int num;
struct node *head,*p;
printf("第一次使用么?是的话请输入1,不是的话请输入0\n");
scanf("%d",&d);
switch(d)
{
case 1:
head=NULL;
while(1)
{
system("cls");
menu();
scanf("%d",&i);
switch(i)
{
case 1:
fflush(stdin);
system("cls");
p=(struct note*)malloc(LEN);
printf("请输入身份证: ");
scanf("%ld",&p->guest.ID);
printf("请输入姓名: ");
scanf("%s",&p->guest.name);
fflush(stdin);
printf("请输入性别:");
scanf("%c",&p->guest.xb);
fflush(stdin);
printf("请输入电话号码:");
scanf("%s",&p->guest.telephone);
printf("请输入入住日期:");
scanf("%d%d%d",&p->guest.check_inyear,&p->guest.check_inmonth,&p->guest.check_inday);
p->guest.check_outyear=0;
p->guest.check_outmonth=0;
p->guest.check_outday=0;
printf("请输入房间号:");
scanf("%s",&p->guest.room);
if(head==NULL)
{
head=regist(p);
}
else
{
head=insert(head,p);
}
printf("登记完毕!\n");
system("pause");
break;
case 2:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
change(head,num);
system("pause");
break;
case 3:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
seek(head,num);
system("pause");
break;
case 4:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
head=shanchu(head,num);
system("pause");
break;
case 5:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
check_out(head,num);
system("pause");
break;
case 6:
system("cls");
print(head);
system("pause");
break;
case 7:
system("cls");
printf("请输入入住日期: ");
scanf("%d%d%d",&a,&b,&c);
keyword(head,a,b,c);
system("pause");
break;
case 8:
save(head);
system("pause");
break;
default :
printf("error!\n");
exit(0);
}
}
case 0:
head=load();
while(1)
{
system("cls");
menu();
scanf("%d",&i);
switch(i)
{
case 1:
fflush(stdin);
system("cls");
p=(struct note*)malloc(LEN);
printf("请输入身份证: ");
scanf("%ld",&p->guest.ID);
printf("请输入姓名: ");
scanf("%s",&p->guest.name);
fflush(stdin);
printf("请输入性别:");
scanf("%c",&p->guest.xb);
fflush(stdin);
printf("请输入电话号码:");
scanf("%s",&p->guest.telephone);
printf("请输入入住日期:");
scanf("%d%d%d",&p->guest.check_inyear,&p->guest.check_inmonth,&p->guest.check_inday);
p->guest.check_outyear=0;
p->guest.check_outmonth=0;
p->guest.check_outday=0;
printf("请输入房间号:");
scanf("%s",&p->guest.room);
if(head==NULL)
{
head=regist(p);
}
else
{
head=insert(head,p);
}
printf("登记完毕!\n");
system("pause");
break;
case 2:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
change(head,num);
system("pause");
break;
case 3:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
seek(head,num);
system("pause");
break;
case 4:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
head=shanchu(head,num);
system("pause");
break;
case 5:
system("cls");
printf("请输入身份证号码: ");
scanf("%ld",&num);
check_out(head,num);
system("pause");
break;
case 6:
system("cls");
print(head);
system("pause");
break;
case 7:
system("cls");
printf("请输入入住日期: ");
scanf("%d%d%d",&a,&b,&c);
keyword(head,a,b,c);
system("pause");
break;
case 8:
save(head);
system("pause");
break;
default :
printf("error!\n");
exit(0);
}
}
}
system("pause");
}因为是新手,也不知道哪里错了,但是信息的添加,修改,删除和查看都是OK的,就是数据保存和信息装载的时候出问题。







