链表中的东西写到文件中读出来的问题
#include <stdio.h>
#include <malloc.h>
typedef struct wage_info
{
char name[20];
char department[100];
float base_pay;
float allowance;
float total;
struct wage_info *next;
}wage;
main()
{
wage *head,*p,*s;
FILE *fp;
float c;
if((fp=fopen("paydata","a+"))==NULL)
{
printf("file is not exist.\n");
fp=fopen("paydata","wb");
}
fp=fopen("paydata","a+");
head=(wage *)malloc(sizeof(wage));
p=head;
printf("Please input the name department base_pay allowance,ends with name is 0:\n");
do
{
s=(wage *)malloc(sizeof(wage));
scanf("%s,%s,%f,%f",&s->name,&s->department,&s->base_pay,&s->allowance);
c=(s->base_pay)+(s->allowance);
printf("%s %s %f %f %f",s->name,s->department,s->base_pay,s->allowance,c);
p->next=s;
p=s;
}while(p->base_pay!=0);
p->next=NULL;
head=head->next;
free(p);
if(fwrite(head,sizeof(wage),1,fp)!=1)
{
printf("can not write files!\n");
}
fclose(fp);
fp=fopen("paydata","rb");
fread(head,sizeof(wage),1,fp);
fclose(fp);
getch();
}
#include <malloc.h>
typedef struct wage_info
{
char name[20];
char department[100];
float base_pay;
float allowance;
float total;
struct wage_info *next;
}wage;
main()
{
wage *head,*p,*s;
FILE *fp;
float c;
if((fp=fopen("paydata","a+"))==NULL)
{
printf("file is not exist.\n");
fp=fopen("paydata","wb");
}
fp=fopen("paydata","a+");
head=(wage *)malloc(sizeof(wage));
p=head;
printf("Please input the name department base_pay allowance,ends with name is 0:\n");
do
{
s=(wage *)malloc(sizeof(wage));
scanf("%s,%s,%f,%f",&s->name,&s->department,&s->base_pay,&s->allowance);
c=(s->base_pay)+(s->allowance);
printf("%s %s %f %f %f",s->name,s->department,s->base_pay,s->allowance,c);
p->next=s;
p=s;
}while(p->base_pay!=0);
p->next=NULL;
head=head->next;
free(p);
if(fwrite(head,sizeof(wage),1,fp)!=1)
{
printf("can not write files!\n");
}
fclose(fp);
fp=fopen("paydata","rb");
fread(head,sizeof(wage),1,fp);
fclose(fp);
getch();
}