高手请来解决xxxxx不能为read 这样的错误,谢谢

pbdwadr 2006-07-07 10:37:13
这是老师布置的一道题,就是学籍管理程序,我写完后,测试时老是出现: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;
}
...全文
194 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
pbdwadr 2006-07-09
  • 打赏
  • 举报
回复

刚才弄成非技术分了,晕死了...
技术分在这里接
http://community.csdn.net/Expert/topic/4868/4868608.xml?temp=.8115808
pbdwadr 2006-07-08
  • 打赏
  • 举报
回复
谢谢各位帮忙,
问题由A_B_C_ABC(黄瓜)解决了,厉害...
不过由于分数太少了
只好先给WingForce (2)、 chenhu_doc (40)、 lyskyly (8)、 三位了

请A_B_C_ABC(黄瓜)到
http://community.csdn.net/Expert/topic/4868/4868508.xml?temp=.621731
去接 分吧 :P (不过说实话,分数其实无所谓的...)

谢谢你们了...
呵呵
0黄瓜0 2006-07-08
  • 打赏
  • 举报
回复
楼主程序一个突出的问题是对内存的分配和释放概念不清,不该分配的地方分配,不该释放的地方释放。

下面的修改后的程序,修改程序好累人哦。


#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

#define NULL 0
#define LEN sizeof(struct student)

struct student
{
char xuehao[20];
char name[10];
char sex[5];
//char age[2];怎么能用2字节表示年龄呢,输入10以上就越界
//int age;
char age[5];
struct student *next;
};

struct student *head,*end; //尽量不要用全局变量
int n; //学生个数
struct student *create()
{
int num;
struct student *p1=0;//,*p2;
n = 0;
head =end= NULL;
//p2 = (struct student *)malloc(LEN);
printf("请输入学生的总数:\n");
scanf("%d", &num);
if(num<=0) return head;
printf("\n请依次输入各个学生的信息:(学号,姓名,性别,年龄)\n");
printf("学生1:\n");
p1 = (struct student *)malloc(LEN);
scanf("%s%s%s%s",p1->xuehao,p1->name,p1->sex,p1->age);
p1->next=NULL;
head=end=p1;
--num;
++n;

while(num)
{
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->next=p1;
end=p1;
++n;
--num;
};
//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);这里不能free,不然你添加到链表中的就变成野指针了
}

int compare(char *A, char *B) //修改后的程序中没使用
{
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 *pre,*cur;
//p = (struct student *)malloc(LEN);这里为什么要分配内存呢?
pre=cur = head;
if(strcmp(cXH,head->xuehao )==0)
{
head=head->next;
free(cur);
return;
}
cur=cur->next;
while(cur)
{
if(strcmp(cXH,cur->xuehao)==0)
{
pre->next=cur->next;
free(cur);
return;
}
else
{
pre=cur;
cur=cur->next;
}
}
printf("你输入的学号不存在。");
/**********************
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)
while((p) != NULL)
{
printf("\t%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;
}

chenhu_doc 2006-07-07
  • 打赏
  • 举报
回复
用的什么ide? tc?
chenhu_doc 2006-07-07
  • 打赏
  • 举报
回复
把错误贴出来?!
chenhu_doc 2006-07-07
  • 打赏
  • 举报
回复
偶刚才也发现了,在选择查看信息的时候,自动跳过了 !
pbdwadr 2006-07-07
  • 打赏
  • 举报
回复
老大
还是不行啊
你运行后,输入几个记录
然后选4.(输出所有记录),还会出现不能为read这样的错误啊
头疼...
lyskyly 2006-07-07
  • 打赏
  • 举报
回复
while(--num)
这有逻辑问题
比如说当num等于1的时候
--num 为0;跳过循环代码,end未被初始化,有逻辑错误
chenhu_doc 2006-07-07
  • 打赏
  • 举报
回复
说明一点, 在vc中c的变量定义都要放在函数的最前面,不然程序就会非常诡异的显示missing ; before type 的error!
chenhu_doc 2006-07-07
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#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");
// printf("请一次输入您要添加的学生的记录");
struct student * pAdd = (struct student *)malloc(LEN);
printf("\n请依次输入您要添加的学生的记录:(学号,姓名,性别,年龄):\n");
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];
struct student *p;
struct student *temp;
printf("\n请输入您要删除的学生的学号\n");
// scanf("%s",cXH);
gets(cXH);

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("此学号不存在 !");
}
}

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;
}
WingForce 2006-07-07
  • 打赏
  • 举报
回复
pclint

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧