高手请来解决xxxxx不能为read 这样的错误,谢谢
这是老师布置的一道题,就是学籍管理程序,我写完后,测试时老是出现:xxxxx不能为read 这样的错误,改了半天也没改好,希望大家帮帮忙,谢谢了^_^
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
char xuehao[20];
char name[10];
char sex[5];
char age[2];
struct student *next;
};
struct student *head,*end;
int n;
struct student *create()
{
int num;
struct student *p1,*p2;
n = 0;
head = NULL;
p1 = (struct student *)malloc(LEN);
p2 = (struct student *)malloc(LEN);
printf("请输入学生的总数:\n");
scanf("%d", &num);
printf("\n请依次输入各个学生的信息:(学号,姓名,性别,年龄)\n");
printf("学生1:\n");
scanf("%s%s%s%s",p1->xuehao,p1->name,p1->sex,p1->age);
while(--num)
{
++n;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p1 = (struct student *)malloc(LEN);
printf("学生%d:\n",n+1);
scanf("%s%s%s%s",p1->xuehao,p1->name,p1->sex,p1->age);
p1->next = NULL;
end = p1;
}
free(p1);
return head;
}
void Logout() //0
{
printf("\n成功退出 !\n");
}
void Add() //1
{
printf("\n请依次输入您要添加的学生的记录:(学号,姓名,性别,年龄):\n");
struct student *pAdd;
pAdd = (struct student *)malloc(LEN);
scanf("%s%s%s%s",pAdd->xuehao,pAdd->name,pAdd->sex,pAdd->age);
pAdd->next = NULL;
end->next = pAdd;
end = pAdd;
free(pAdd);
}
int compare(char *A, char *B) //used in Delete()
{
while((int)*A++ == (int)*B++)
{
;
}
if((*A == NULL)&&(*B == NULL))
{
return 1;
}
else
{
return 0;
}
}
void Delete() //2
{
char cXH[20];
printf("\n请输入您要删除的学生的学号\n");
scanf("%s",cXH);
struct student *p;
p = (struct student *)malloc(LEN);
p = head;
if(compare(cXH,head->xuehao) == 1) //判断是不是第一个
{
struct student *temp;
temp = (struct student *)malloc(LEN);
temp = head;
head = head->next;
free(temp);
printf("成功删除 !\n");
}
else
{
while((p->next->next) != NULL)
{
if(compare(cXH, p->next->xuehao) == 1)
{
break;
}
p = p->next;
if(p->next->next == NULL)
{
printf("此学号不存在 !");
}
}
struct student *temp;
temp = (struct student *)malloc(LEN);
temp = p->next;
p->next = p->next->next;
free(temp);
printf("成功删除 !\n");
}
free(p);
}
void Search() //3
{
printf("\n请选择查询方式:\n0.退出\n1.按学号\n2.按姓名\n");
}
void Display()//4
{
struct student *p = (struct student *)malloc(LEN);
p = head;
printf("学生信息如下:\n");
printf("\t学号\t姓名\t性别\t年龄\n");
while((p->next) != NULL)
{
printf("%s\t%s\t%s\t%s\n",p->xuehao,p->name,p->sex,p->age);
p = p->next;
}
free(p);
}
int main()
{
int commend=-1;
struct student *pStu;
pStu = (struct student *)malloc(LEN);
pStu = create();
while(commend != 0)
{
printf("\n\n请选择您想执行的操作:\n0.退出\n1.添加一个记录\n2.删除一个记录\n3.查询\n4.显示所有记录\n\n");
scanf("%d", &commend);
switch(commend)
{
case 0:
Logout();
system("pause");
break;
case 1:
Add();
break;
case 2:
Delete();
break;
case 3:
Search();
break;
case 4:
Display();
break;
default :
printf("无效指令 !");
break;
}
}
return 0;
}