标题:链表中的东西写到文件中读出来的问题
取消只看楼主
gxppa2006
Rank: 1
等 级:新手上路
帖 子:178
专家分:0
注 册:2007-2-27
 问题点数:0 回复次数:0 
链表中的东西写到文件中读出来的问题
#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();
}
搜索更多相关主题的帖子: 链表 wage float 文件 
2007-04-14 23:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-131944-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054283 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved