insert08() /* 插入模块 */
{
int m,n,a=-1;
char str[40];
float t;
if((fp=fopen("s_score.txt","rb+"))==NULL||(fp1=fopen("temp.txt","wb+"))==NULL)
{
printf("无法打开该文件.\n");
getch();exit(0);
}
for(i=0;i<30;i++)
{
if((fread(&info[i],sizeof(info[i]),1,fp))!=1)
break;
}
printf("在哪个学号之后插入信息:");
scanf("%d",&m);
while((fread(&data,sizeof(data),1,fp))==1)
{
n=atoi(data.num);
do
{
a+=1;
}while(m!=n);
for(j=i;j>a;j--)
{
strcpy(info[j+1].num,info[j].num);
strcpy(info[j+1].name,info[j].name);
strcpy(info[j+1].sex,info[j].sex);
strcpy(info[j+1].birthday.year,info[j].birthday.year);
strcpy(info[j+1].birthday.month,info[j].birthday.month);
strcpy(info[j+1].birthday.day,info[j].birthday.day);
strcpy(info[j+1].room,info[j].room);
strcpy(info[j+1].tel,info[j].tel);
info[j+1].math=info[j].math;
info[j+1].English=info[j].English;
info[j+1].C=info[j].C;
info[j+1].score=info[j].score;
}
printf("输入插入的信息:");
Reapeat:
printf("\n请输入学号:");
gets(info[a+1].num);
if(*info[a+1].num==NULL) goto Reapeat;
printf("请输入姓名:");
gets(info[a+1].name);
printf("请输入性别:");
gets(info[a+1].sex);
printf("请输入出生年月日:");
gets(info[a+1].birthday.year);gets(info[a+1].birthday.month);gets(info[a+1].birthday.day);
printf("请输入宿舍号:");
Again:
gets(info[a+1].room);
if(*info[a+1].room==NULL) goto Again;
printf("请输入电话号码:");
gets(info[a+1].tel);
printf("请输入高数成绩:");
gets(temp);info[a+1].math=atof(temp);
printf("请输入英语成绩:");
gets(temp);info[a+1].English=atof(temp);
printf("请输入C语言成绩:");
gets(temp);info[a+1].C=atof(temp);
info[a+1].score=info[a+1].math+info[a+1].English+info[a+1].C;
fwrite(&data,sizeof(data),1,fp1);
}
fseek(fp,0L,0);
fseek(fp1,0L,0);
while((fread(&data,sizeof(data),1,fp1))==1)
{
fwrite(&data,sizeof(data),1,fp);
}
fclose(fp);
fclose(fp1);
}
这是我用数组编的一个学生管理系统中的插入模块.要求是可以把新的信息插入到合适位置,比如某学号后.但是我编的这个模块在输入了所要插入的位置即"printf("在哪个学号之后插入信息:");scanf("%d",&m);"后就会跳出程序不再运行.还望各位大虾指教一下错在哪或者告诉小弟一个更好的方法.小弟在此先谢过了.
****************************************
[此贴子已经被作者于2006-9-18 15:30:56编辑过]