这到底是为什么啊(重分求解)
bnbn3 2004-05-02 09:05:38 我的代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void input(void);
struct student
{char name[20];
int num;
int chinese;
int maths;
int zhongfen;
int paiming;
};
struct student *p,*q;
int a;
void main()
{
int b;
printf("please input:1:please input shuju.2:review the paiming.3:find the paiming.4:exit.");
scanf("%d",&b);
switch(b)
{case 1:input();break;
case 2: break;
case 3:exit(0);break;
}
}
void input(void)
{void charupaixu(void);
int m;
char n[20];
printf("please input name: \n" );
printf("please input number: \n");
printf("please input chinese: \n");
printf("please input maths: \n");
m=wherey();
gotoxy(22,m-4);
do
{p=(struct student*)malloc(sizeof(struct student));
a++;
scanf("%s",p[a].name);
if((strcmp(p[a].name,"tuichu"))==0)break;
gotoxy(22,m-3);
scanf("%d",&p[a].num);
gotoxy(22,m-2);
scanf("%d",&p[a].chinese);
gotoxy(22,m-1);
scanf("%d",&p[a].maths);
gotoxy(22,m-4);
free(p);
p[a].zhongfen=p[a].chinese+p[a].maths;
charupaixu();}
while((strcmp(p[a].name,"tuichu"))!=0);
}
void charupaixu(void)
{FILE *fp;
int b=0,d,c,e;
char n[20];
fp=fopen("c:\shuju","wb");
if(feof(fp))
fwrite(&p[a],sizeof(struct student),1,fp);我检查了一便,前面的都 对,问题 就出在这儿
while(!feof(fp)) fwrite函数写入失败,每次都是
{q=(struct student*)malloc(sizeof(struct student));这样导至后面的程序出错,
b++; 可我看了半天也不知
fread(&q[b],sizeof(struct student),1,fp); 为什么会写入失败
free(q);}
for(d=1;d<=b;d++)
if(p[a].zhongfen>=q[d].zhongfen&&p[a].zhongfen<=q[d+1].zhongfen)
{for(c=b;c>d;c--)
{q=(struct student*)malloc(sizeof(struct student));
strcpy(q[c+1].name,q[c].name);q[c+1].num=q[c].num;q[c+1].chinese=q[c].chinese;
q[c+1].maths=q[c].maths;}
free(q);break;
}
for(e=1;e<=b+1;e++)
fwrite(&q[e],sizeof(struct student),1,fp);
fclose(fp);
}