首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

什么保存了之后再读取然后输出就不对?该如何解决

2012-02-12 
什么保存了之后再读取然后输出就不对?????????什么保存了之后再读取然后输出就不对?????????#include std

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

#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);}

[解决办法]
改为sizeof(struct student)就可以正常运行
[解决办法]
sizeof(LEN)
==》
LEN

你的 LEN 宏定义不就是 结构体的长度么 ······

热点排行