求大神修改!无法保存该c语言的数据!怎样修改才能在第一次输入的数据能在第二次打开时被读取!
#include <stdio.h>#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define ID struct id
struct id
{
char name[20];
int num;
char price[20];
char sum[20];
ID *next;
}Market;
struct id *head=NULL;
int pc=0;
void save();
int read();
ID *creat()
{
ID *p1,*p2,*head;
int pd;
p1=p2=head=NULL;
printf("请输入数字(0为终止符)!\n");
printf("*********************************************************\n");
while(1)
{
printf("请输入商品编号:\n");scanf("%d",&pd);
if(pd==0) break;
p1=(ID*)malloc(sizeof(ID));
p1->num=pd;
printf("请输入商品名称:\n");scanf("%s",p1->name);
printf("请输入商品单价:\n");scanf("%s",p1->price);
printf("请输入商品总量:\n");scanf("%s",p1->sum);
if(head==NULL)
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
pc++;
}
p2->next=NULL;
return(head);
}
ID *insert(ID *head)
{
ID *temp,*p1,*p2;
printf("开始插入!!!\n");
temp=(ID *)malloc(sizeof(ID));
printf("请输入商品编号:\n");scanf("%d",&temp->num);
printf("请输入商品名称:\n");scanf("%s",temp->name);
printf("请输入商品单价:\n");scanf("%s",temp->price);
printf("请输入商品总量:\n");scanf("%s",temp->sum);
if(head==NULL)
{
head=temp;
temp->next=NULL;
}
else
{
p1=head;
while(p1!=NULL)
{
p2=p1;
p1=p1->next;
}
p2->next=temp;
temp->next=p1;
}
printf("成功插入!");
pc++;
return(head);
}
ID *delet(ID *head)
{
ID *p1,*p2;
int num;
printf("请输入要删除货物的编号:");scanf("%d",&num);
p1=head;
if(head==NULL)
{
printf("无历史纪录\n");
goto end;
}
while(num!=p1->num&&p1!=NULL)
{
p2=p1;p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("成功删除!!!\n");
pc--;
}
end:return head;
}
ID *search_name(ID *head)
{
ID *p1,*p2;
char str[100];
printf("请输入要查找货物的编号:");scanf("%s",str);
p1=head;
while(strcmp(str,p1->name)&&p1!=NULL)
{
p2=p1;p1=p1->next;
}
if(strcmp(str,p1->name)==0)
{
printf("商品编号:%d\n",p1->num);
printf("商品名称:%s\n",p1->name);
printf("商品单价:%s\n",p1->price);
printf("商品总量:%s\n",p1->sum);
}
return head;
}
ID *search_num(ID *head)
{
int num;
ID *p1,*p2;
printf("请输入要货物的编号:");scanf("%d",&num);
p1=head;
while(num!=p1->num&&p1!=NULL)
{
p2=p1;p1=p1->next;
}
if(num==p1->num)
{
printf("商品编号:%d\n",p1->num);
printf("商品名称:%s\n",p1->name);
printf("商品单价:%s\n",p1->price);
printf("商品总量:%s\n",p1->sum);
}
return head;
}
ID *search(ID *head)
{
int mode;
ID *h;
printf("请选择搜索方式:方法1.num 方法2.name\n");
scanf("%d",&mode);
if(mode==1)
h=search_num(head);
else if(mode==2)
h=search_name(head);
else
printf("输入错误!\n");
return h;
}
void print(ID *head)
{
ID *p;
p=head;
printf("\t\t\t******************\n");
printf("结果列表显示 :\n");
if(head!=NULL)
do
{
printf("编号%-10d 名称%-10s 单价%-10s 总量%-10s\n",p->num,p->name,p->price,p->sum);
p=p->next;
}while(p!=NULL);
}
void save()//保存收入
{
FILE *fp;
if((fp=fopen("d:\\market.din","wb"))==NULL)
{
printf("cannot open file\n");
exit(1);
}
fwrite(&head,sizeof(int),1,fp);
if(fwrite(Market,sizeof(struct id),4,fp)!=4)
printf("file write error");
fclose(fp);
}
int read()
{
FILE *fp;
if((fp=fopen("d:\\market.din","rb"))==NULL)
{
return 1;
}
fread(&head,sizeof(int),1,fp);
if(fread(Market,sizeof(struct id),4,fp)!=4)
{
printf("read error");
exit (1);}
fclose(fp);
}
void main()
{
ID *head=NULL;
int choise;
head=read();
printf("\t\t*****大型仓库管理系统*****\n");
while(1)
{
printf("\t\t 商品管理系统公测版本\n");
printf("|*********************************************************|\n");
printf("|**| 1.输入 |**|\n");
printf("|**| 2.显示 |**|\n");
printf("|**| 3.搜索 |**|\n");
printf("|**| 4.插入 |**|\n");
printf("|**| 5.删除 |**|\n");
printf("|**| 0.退出 |**|\n");
printf("|*********************************************************|\n");
printf("请选择服务项目(0-6):");
scanf("%d",&choise);
switch(choise)
{
case 1:head=creat();
break;
case 2:print(head);
break;
case 3:head=search(head);
break;
case 4:head=insert(head);
break;
case 5:head=delet(head);
break;
case 0:
exit(0);
break;
default:printf("输入错误! 请重新输入!\n");
}
}save ();
}