什么保存了之后再读取然后输出就不对?????????

sai999 2007-01-12 11:27:56
什么保存了之后再读取然后输出就不对?????????

#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
char name[10];
float mat,eng,com,sum,aver;
struct student *next;
};
int n;
struct student *head;


struct student *creat(void)
{struct student *head;
struct student *p1,*p2;
n=0;
clrscr();
printf("*************************CREAT*******************************\n\n");
p1=p2=(struct student *)malloc(LEN);
printf("please input name:");
scanf("%s",p1->name);
printf("please input number:");
scanf("%ld",&p1->num);
printf("please input math:");
scanf("%f",&p1->mat);
printf("please input english:");
scanf("%f",&p1->eng);
printf("please input computer:");
scanf("%f",&p1->com);
p1->sum=p1->mat+p1->eng+p1->com;
p1->aver=p1->sum/3;
head=NULL;
while(1)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
printf("please input name:");
scanf("%s",p1->name);
if((strcmp(p1->name,"0")==0))return(head);
printf("please input number:");
scanf("%ld",&p1->num);
printf("please input math:");
scanf("%f",&p1->mat);
printf("please input english:");
scanf("%f",&p1->eng);
printf("please input computer:");
scanf("%f",&p1->com);
p1->sum=p1->mat+p1->eng+p1->com;
p1->aver=p1->sum/3;
}
p2->next=NULL;
return(head);
}
void save(struct student *head)
{
FILE *fp;
struct student *p;
char outfile[30];
printf("Enter outfile name,for example c:\\student\\c.txt:\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("can not open the file\npress angykey out!\n");

getch();
exit(0);
}
printf("\nSaving the file......\n");
p=head;
while(p!=NULL)
{
fwrite(p,sizeof(LEN),1,fp);
p=p->next;
}
fclose(fp);
printf("-----save success!!-----\nenter anykey out!\n");
getch();
}




struct student *load()
{
struct student *p,*q,*h=NULL;
FILE *fp;
char infile[30];
printf("Enter infile name,for example c:\\student\\c.txt:\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
{
printf("can not open file\nenter anykey out!\n");
getch();
exit(1);
}
printf("\n -----Loading file!-----\n");
p=(struct student *)malloc(sizeof(LEN));
if(!p)
{
printf("error!\n");
return h;
}
h=p;
while(!feof(fp))
{
if(1!=fread(p,sizeof(LEN),1,fp))
break;
p->next=(struct student *)malloc(sizeof(LEN));
if(!p->next)
{
printf("error!\n");
return h;
}
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("---Load success !!!---\nenter anykey out!\n");
getch();
return h;
}


void print(struct student *head)
{struct student *p;
printf("****************************ALL STUDENTS****************************\n");
printf("number name math english computer sum aver\n");
p=head;
if(p!=NULL)
do
{printf("%-10ld %-10s %-5.1f %-5.1f %-5.1f %-5.1f %-6.2f\n",p->num,p->name,p->mat,p->eng,p->com,p->sum,p->aver);
p=p->next;
}while(p!=NULL);
printf("press anykey out!\n");
getch();
}
main()
{float aaa=9,b;
b=sqrt(aaa);
head=creat();save(head);head=load();print(head);}
...全文
176 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iu_81 2007-01-12
  • 打赏
  • 举报
回复
各科成绩均为0.0
guochun 2007-01-12
  • 打赏
  • 举报
回复
把输入输出贴出来看看
qinxiaofei 2007-01-12
  • 打赏
  • 举报
回复
好长
Sco_field 2007-01-12
  • 打赏
  • 举报
回复
这么长。。。帮顶接分~谢谢
jixingzhong 2007-01-12
  • 打赏
  • 举报
回复
sizeof(LEN)
==》
LEN

你的 LEN 宏定义不就是 结构体的长度么 ······
iu_81 2007-01-12
  • 打赏
  • 举报
回复
改为sizeof(struct student)就可以正常运行
iu_81 2007-01-12
  • 打赏
  • 举报
回复
sizeof(LEN)有问题

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧