麻烦高手帮我看下 程序没错 但输出的时候说什么内存不能读取
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#define NULL 0
struct student
{
int xh;
char xm[10];
int nl;
char xb[10];
char bj[8];
struct student *next;
}stu[4];
int n;
///////////////////////////////////////
void input()
{
FILE *fp;
int i;
fp=fopen("e:\\in.txt","w");
printf("请输入学号,姓名,年龄,性别,班级\n");
for(i=0;i<2;i++)
{
scanf("%s%s%d%s%s",&stu[i].xh,stu[i].xm,&stu[i].nl,stu[i].xb,stu[i].bj);
fprintf(fp,"%s%s%d%s%s\n",stu[i].xh,stu[i].xm,stu[i].nl,stu[i].xb,stu[i].bj);
}
fclose(fp);
}
////////////////////////////////
void ReadDat()
{
FILE *fp;
int i;
if((fp=fopen("e:\\in.txt","r"))==NULL)
{
printf("cannot open file");
exit(0);
}
for(i=0;i<2;i++)
{
fscanf(fp,"%s%s%d%s%s",&stu[i].xh,stu[i].xm,&stu[i].nl,stu[i].xb,stu[i].bj);
printf("%s%s%d%s%s\n",stu[i].xh,stu[i].xm,stu[i].nl,stu[i].xb,stu[i].bj);
}
fclose(fp);
}
/////////////////////////////////
struct student * CreatStu(struct student *head)
{
int i,j,t;
for(i=0;i<2;i++)
for(j=0;j<2-i;j++)
if(stu[j].xh>stu[j+1].xh )
{
t=stu[j].xh;
stu[j].xh=stu[j+1].xh;
stu[j+1].xh=t;
}
struct student *p1,*p2;
p1=p2=(struct student*)malloc(sizeof(struct student));
printf("请输入数据\n");
p1->xh=stu[i].xh;
strcpy(p1->xm,stu[i].xm);
p1->nl=stu[i].nl;
strcpy(p1->xb,stu[i].xb);
strcpy(p1->bj,stu[i].bj);
p1->next=NULL;
head=p1;
p2=p1;
while(head!=NULL)
{
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
p1->xh=stu[i].xh;
strcpy(p1->xm,stu[i].xm);
p1->nl=stu[i].nl;
strcpy(p1->xb,stu[i].xb);
strcpy(p1->bj,stu[i].bj);
p1->next=NULL;
p2->next=p1;
p2=p1;
}
return head;
}
/////////////////////////////////////////
void DisplayStu(struct student *head)
{
struct student *temp;
temp=head;
while(temp!=NULL)
{
printf("\n%s%s%d%s%s\n",temp->xh,temp->xm,temp->nl,temp->xb,temp->bj);
temp=temp->next;
}
}
//////////////////////////////////////
struct student *InsertStu( struct student *head)
{
struct student *p1,*p2,*p3;
p1=(struct student*)malloc(sizeof(struct student));
p1->xh=n;
p2=head;
if(head=NULL)
{
head=p1;
p1->next=NULL;
}
else
{
while(n>p2->xh&&p2->next!=NULL)
{
p3=p2;
p2=p2->next;
}
if(n<=p2->xh)
if(head==p2)
{
head=p1;
p1->next=p2;
}
else
{
p3->next=p1;
p1->next=p2;
}
else
{
p2->next=p1;
p1->next=NULL;
}
}
return head;
}
/////////////////////////////////////////////////////////////////////
struct student * DeleteStu(struct student *head)
{
struct student *temp,*p;
temp=head;
if(head==NULL)
printf("只是空表\n");
else
{
temp=head;
while(strcmp(temp->xb,"nan")!=0&&temp->next!=NULL)
{
p=temp;
temp=temp->next;
}
if(strcmp(temp->xb,"nan")==0)
{
if(temp==head)
{
head=head->next;
free(temp);
}
else
{
p->next=temp->next;
free(temp);
}
}
}
return head;
}
//////////////////////////////////
void WriteDat (struct student *head)
{
printf("已写入out文件\n");
FILE *fp;
struct student *p;
p=head;
fp=fopen("e:\\out.txt","w");
while(p!=NULL)
{
fprintf(fp,"%d %s %d %d %s \n",p->xh,p->xm,p->nl,p->xb,p->bj);
p=p->next;
}
}
///////////////////////////////////////
void main()
{
struct student *head;
head=NULL;
struct student *insert();
struct student *delet();
struct student *creat();
input();
head=CreatStu(head);
DeleteStu(head);
InsertStu(head);
}