求助高手~~ 访问内存冲突(分割故障) 是什么原因造成的!!!!

sj307639429 2008-06-16 11:32:01
struct person *del_person (struct person *head)
{
struct person *p1,*p2;
struct person0 *p;
char name[20];
int m=1;
char t;
while(m)
{
fp=fopen("tel_book.txt","rb");
if(fp=NULL){printf("the list is null\n");
exit(0);
}
else
{
p=person;
fread(p,LEN0,1,fp);
if(!feof(fp)){printf("tel_book.txt is empty file!\n");
fclose(fp);
return 0;
}
rewind(fp);
move(fp);
p1=head;
printf("\n Please input the Name for deletion:");
scanf("%s",p1->name);
while(strcmp(name,p1->name)!=0&&p1->next!=NULL)//未找到,并且后面还有结点
{
p2=p1;p1=p1->next;
}
if(strcmp(name,p1->name)==0)
{
if(p1==head)head=p1->next;//p1指向首结点,把第二个结点位置赋予head
else
{p2->next=p1->next;//将下一个结点地址赋给前一结点地址
printf("you have deleted:%s",name);
}
j--;
}
else printf("Can't find the record");
save(head);
}
fclose(fp);
printf(" continue delete?(y / n)\n");
while(getchar()!='\n');
scanf("%c",&t);
if(t!='y')m=0;
return(0);
}
}
/******************************************************************************/


我的main()函数在调用这个含函数是系统说我“访问内存冲突(分割故障) 被提出”
...全文
215 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
chlaws 2008-06-17
  • 打赏
  • 举报
回复
。。。
这么简单还要答辩.
bless
baihacker 2008-06-17
  • 打赏
  • 举报
回复
输入什么,输出什么,是你应该事先设计好的,而不是想到什么写什么.
为什么要这样的输入,输入是什么意思
为什么要这样的输出,输出是什么意思?
sj307639429 2008-06-17
  • 打赏
  • 举报
回复
谭浩强说这样可以删啊~~~~~~~~
长安宁 2008-06-17
  • 打赏
  • 举报
回复
if(strcmp(name,p1->name)==0)
{
if(p1==head)head=p1->next;//p1指向首结点,把第二个结点位置赋予head
else
{p2->next=p1->next;//将下一个结点地址赋给前一结点地址
printf("you have deleted:%s",name);
}
你的删除结点只是把结点让过去了,
while(getchar()!='\n');
scanf("%c",&t);
if(t!='y')m=0;
的时候,等于还是没有 把结点从链中去掉;
sj307639429 2008-06-16
  • 打赏
  • 举报
回复
是不是应该把
int search(struct person *head)
给为
int search(struct person *head)
struct person *search(struct person *head)
sj307639429 2008-06-16
  • 打赏
  • 举报
回复
但是我在做课程设计,要答辩的,自己做会好的吧~~~~~~~~~
baihacker 2008-06-16
  • 打赏
  • 举报
回复
int search(struct person *head) //定义和声明不一致
sj307639429 2008-06-16
  • 打赏
  • 举报
回复
/******************************************************************************/
int show_all()
{
struct person *head;
struct person *p1;
struct person0 *p;
p=person;
int i=0;
p1=head;
fp=fopen("tel_book.txt","rb");
if(fp==NULL){printf("the file is null\n");
exit(0);
}
else
{for(;i<j;i++)
{
fread((p+i),LEN0,1,fp);
printf(" The name is: %s \n",(p+i)->name0);
printf(" The relation is: %s \n",(p+i)->relation0);
printf(" The address is: %s \n",(p+i)->address0);
printf(" The tel_number is: %s \n",(p+i)->tel_number0);
printf(" The post is:%s\n",(p+i)->post0);
printf(" The qq is:%ld \n",(p+i)->qq0);
}
}
fclose(fp);
return 0;
}
/******************************************************************************/
int main()
{
int m;
struct person *head=NULL;
printf("***************************************************************\n");
printf("***************************************************************\n");
printf(" \n");
printf(" Mini Address Book \n");
printf("---------------------------------------------------------------\n");
printf(" 1.Add preson 4. modify information \n");
printf(" 2.Delete person 5.Inquire person \n");
printf(" 3.Show all 0.Close Address book \n");
printf("---------------------------------------------------------------\n");
printf(" -------------- Made by sujun \n");
printf("***************************************************************\n");
printf("***************************************************************\n");
printf("Please input a number \n");
scanf("%d",&m);
system ("cls");
switch(m)
{
case 1:{head=creat();/*执行添加函数*/
head=alter(head);}/*添加完成后排序*/
break;
case 2:{head=del_person(head);/*执行删除函数*/
head=alter(head);}/*执行完成后排序*/
break;
case 3:show_all();/*执行显示所有函数*/
break;
case 4:{alter(head);/*执行修改信息函数*/
head=alter(head);}/*修改信息后排序*/
break;
case 5:search(head);/*执行查询函数*/
break;
case 0:break;/*关闭通讯录*/
defalt:printf("Your choice is wrong,please choose one more time\n");
}
return 0;
}
chlaws 2008-06-16
  • 打赏
  • 举报
回复
汗,原来是这,直接去我blog下吧,N久之前就有了。
sj307639429 2008-06-16
  • 打赏
  • 举报
回复
/******************************************************************************/
struct person *del_person (struct person *head)
{
struct person *p1,*p2;
struct person0 *p;
char name[20];
int m=1;
char t;
while(m)
{
fp=fopen("tel_book.txt","rb");
if(fp=NULL){printf("the list is null\n");
exit(0);
}
else
{
p=person;
fread(p,LEN0,1,fp);
if(!feof(fp)){printf("tel_book.txt is empty file!\n");
fclose(fp);
return 0;
}
rewind(fp);
move(fp);
p1=head;
printf("\n Please input the Name for deletion:");
scanf("%s",p1->name);
while(strcmp(name,p1->name)!=0&&p1->next!=NULL)//未找到,并且后面还有结点
{
p2=p1;p1=p1->next;
}
if(strcmp(name,p1->name)==0)
{
if(p1==head)head=p1->next;//p1指向首结点,把第二个结点位置赋予head
else
{p2->next=p1->next;//将下一个结点地址赋给前一结点地址
printf("you have deleted:%s",name);
}
j--;
}
else printf("Can't find the record");
save(head);
}
fclose(fp);
printf(" continue delete?(y / n)\n");
while(getchar()!='\n');
scanf("%c",&t);
if(t!='y')m=0;
return(0);
}
}
/******************************************************************************/
struct person *alter(struct person *head)
{
struct person *p1,*p2;
char b;
char name[20];
printf("please input the name that you want to change:\n");
gets(name);
if(head==NULL)
{
printf("list null!\n");
}
p1=head;
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
printf("*****************************\n");
printf("the name:%s\n",p1->name);
printf("the relation:%s\n",p1-> relation);
printf("the address:%s\n",p1->address);
printf("the tel_number:%s\n",p1-> tel_number);
printf("the post:%s\n",p1-> post);
printf("the qq:%d\n",p1-> qq);
printf("*****************************\n");
while(1)
{
printf("*********************************\n");
printf("** please choose the option **\n");
printf("** 1-change the name **\n");
printf("** 2-change the relation **\n");
printf("** 3-change the address **\n");
printf("** 4-change the tel_number **\n");
printf("** 5-change the post **\n");
printf("** 6-change the qq **\n");
printf("** 7-exit **\n");
printf("*********************************\n");
scanf("%c",&b);
switch(b)
{
case '1': printf("NOW yuo will change the name:\n");
gets(p1->name);
break;
case '2':printf("NOW you will change the relation :\n");
gets(p1->relation);
break;
case '3':printf("NOW you will change the address:\n");
gets(p1-> address);
break;
case '4':printf("NOW you will change the tel_number :\n");
gets(p1-> tel_number );
break;
case '5':printf("NOW you will change the post :\n");
gets(p1-> post );
break;
case'6':printf("NOW you will change the qq :\n");
scanf("%d\n",&p1->qq);
break;
case'7':break;
default:printf("Error\n");
}
}
}
}
/******************************************************************************/
int search(struct person *head)
{
struct person *p1;
char name[20];
printf("please input the name whose you want to see:\n");
gets(name);
if(head==NULL)
{
printf("list is null!\n");
}
p1=head;
for(;p1!=NULL;p1=p1->next)
{
if(strcmp(p1->name,name)==0)
{printf("*****************************\n");
printf("the name:%s\n",p1->name);
printf("the relation :%s\n",p1-> relation );
printf("the address:%s\n",p1-> address);
printf("the tel_number:%s\n",p1-> tel_number);
printf("the post:%s\n",p1-> post);
printf("the qq:%d\n",p1->qq);
printf("*****************************\n");
}
else
printf("Don’t have the person!!!\n");
}
return(0);
}
/******************************************************************************/
struct person *array(struct person *head)
{
struct person *p1,*p2;
char name[20];
char relation[20];
char address[20];
char tel_number[20];
char post[20];
unsigned long qq;
for(p1=head;p1!=NULL;p1=p1->next)
{
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if((strcmp(p1->name,p2->name))>0)
{
strcpy(name,p1->name);
strcpy(relation,p1-> relation);
strcpy(address,p1-> address);
strcpy(tel_number,p1-> tel_number);
strcpy(post,p1->post);
qq=p1-> qq;
strcpy(p1->name,p2->name);
strcpy(p1-> relation,p2-> relation);
strcpy(p1-> address,p2-> address);
strcpy(p1-> tel_number,p2-> tel_number);
strcpy(p1-> post,p2-> post);
p1-> qq=p2-> qq;
strcpy(p2->name,name);
strcpy(p2-> relation, relation);
strcpy(p2-> address, address);
strcpy(p2-> tel_number, tel_number);
strcpy(p2-> post, post);
p2-> qq=qq;
}
}
}
save(head);/*排序后保存信息于磁盘中*/
return(head);
}
/******************************************************************************/
sj307639429 2008-06-16
  • 打赏
  • 举报
回复
/******************************************************************************/
int move(FILE *fp0)
{
struct person *p1,*p2,*head;
struct person0 *p3;
int i=0;
p3=person;
p1=p2=(struct person *)malloc(LEN);
while(1)
{fread(p3+i,LEN0,1,fp0);
if(feof(fp0))break;
strcpy(p1->name,(p3+i)->name0);
strcpy(p1->relation,(p3+i)->relation0);
strcpy(p1->address,(p3+i)->address0);
strcpy(p1->tel_number,(p3+i)->tel_number0);
strcpy(p1->post,(p3+i)->post0);
p1->qq=(p3+i)->qq0;
if(i==0)//首结点
head=p1;
else
{p2->next=p1;
p1=p2;
p1=(struct person *)malloc(LEN);//前插法
}
i++;
j++;
}
p2->next=NULL;
rewind(fp);
free(p1);
return 0;
}
/******************************************************************************/
struct person *creat (void)
{
int n,m=1;
char p;
struct person *p1,*p2;
struct person *head;
while(m)
{
if((fp=fopen("tel_book.txt","rb"))==NULL)
{printf("you need Create a new document!\n");
fp=fopen("Tel_book.txt","w+");
p1=(struct person *)malloc(LEN);
printf("Now input the inserted record:\n");//输入数据
while(getchar()!='\n');
printf("The name:\n");
scanf("%s",p1->name);
printf("The relation:\n");
scanf("%s",p1->relation);
printf("The address:\n");
scanf("%s",p1->address);
printf("the tel_number:\n");
scanf("%s",p1->tel_number);
printf("The post:\n");
scanf("%s",p1->post);
printf("The qq:\n");
scanf("%ld",&p1->qq);
p1->next=NULL;
head=p1;
j=1;
printf("your Add record in tel_book.txt!\n");
save(head);
return 0;
}
else/*前插法添加结点*/
{
fp=fopen("tel_book.txt","rb");
p2=(struct person *)malloc(LEN);
printf("Now input the inserted record:\n");/*输入数据*/
while(getchar()!='\n');
printf("Name:\n");
scanf("%s",p1->name);
printf("Relation:\n");
scanf("%s",p1->relation);
printf("Address:\n");
scanf("%s",p1->address);
printf("Tel_number:\n");
scanf("%s",p1->tel_number);
printf("post:\n");
scanf("%s",p1->post);
printf("qq:\n");
scanf("%ld",&p2->qq);
p2->next=head;/*前插法添加结点*/
head=p2;
j++;
save(head);
}
fclose(fp);
printf(" continue add?(y / n)\n");
while(getchar()!='\n');
scanf("%c",&p);
if(p!='y')m=0;
while(getchar()!='\n');
system ("cls");
}
return 0;
}
/******************************************************************************/
sj307639429 2008-06-16
  • 打赏
  • 举报
回复
我要做一个电子通讯录,在对文件的的读入和写出还有对内存访问时总是会遇到很多问题,希望大家多多指教啊


我的完整代码是:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#define LEN sizeof(struct person)
#define LEN0 sizeof(struct person0)

int j=1;/*结点*/
int n;
FILE *fp;
struct person *creat ();/*添加联系人*/
struct person *del_person();/*删除联系人*/
int show_all();/*显示所有联系人*/
struct person *alter();/*修改信息*/
struct person *search();/*查询联系人*/
struct person *array();/*排序*/
int save(struct person *);/*保存数据于磁盘中*/
struct person *get;/*打开并读取文件内容*/
int move(struct person0 *);//将文件里的信息转移到链表中去

struct person
{
char name[20];/*姓名*/
char relation[20];/*关系*/
char address[20];/*地址*/
char tel_number[20];/*电话号码*/
char post[20];/*邮编*/
unsigned long qq;/*QQ号*/
struct person *next;
};

struct person0
{
char name0[20];/*姓名*/
char relation0[20];/*关系*/
char address0[20];/*地址*/
char tel_number0[20];/*电话号码*/
char post0[20];/*邮编*/
unsigned long qq0;/*QQ号*/
}person[100];//结构体数组用于文件读存的中间转换
/******************************************************************************/
int save(struct person *head)/*保存记录*/
{
struct person *p1;
struct person0 *p2;
int i;
if((fp=fopen("tel_book.txt","wb"))==NULL)
{printf("cannot open file\n");
exit(0);
}
p1=head;
p2=person;
for(i=0;i<j;i++)
{strcpy((p2+i)->name0,p1->name);
strcpy((p2+i)->relation0,p1->relation);
strcpy((p2+i)->address0,p1->address);
strcpy((p2+i)->tel_number0,p1->tel_number);
strcpy((p2+i)->post0,p1->post);
(p2+i)->qq0=p1->qq;
if(fwrite(p2+i,LEN0,1,fp)!=1)
printf("file write error\n");
p1=p1->next;
}
rewind(fp);
free(p1);
return 0;
}
/******************************************************************************/
baihacker 2008-06-16
  • 打赏
  • 举报
回复
if(fp=NULL)//看到一处错
baihacker 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sj307639429 的回复:]
2楼说的对,鉴于此我注释下我的代码;
struct person *del_person (struct person *head)//删除通讯录中的联系人
{
struct person *p1,*p2;
struct person0 *p;
[/Quote]

不用注释也能看懂的,只是告诉你按这些要求来要求自己的编码...
但是你这样写作一堆,确实没有人想看...呵
chlaws 2008-06-16
  • 打赏
  • 举报
回复
PS:建议代码贴全来,并说明要做什么功能,这样别人也能好帮你改
baihacker 2008-06-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yuzl32 的回复:]
乱七八糟的代码..
[/Quote]
呵,确实比较糟糕...但是谁都是从这里走出来.
楼主看看我前两楼的回复,按这些要求改改.
sj307639429 2008-06-16
  • 打赏
  • 举报
回复
2楼说的对,鉴于此我注释下我的代码;
struct person *del_person (struct person *head)//删除通讯录中的联系人
{
struct person *p1,*p2;
struct person0 *p;
char name[20];
int m=1;
char t;
while(m)
{
fp=fopen("tel_book.txt","rb");//以只读方式打开文件
if(fp=NULL){printf("the list is null\n");
exit(0);
}
else
{
p=person;//p指向已定义的结构体数组 person
fread(p,LEN0,1,fp);
if(!feof(fp)){printf("tel_book.txt is empty file!\n");
fclose(fp);
return 0;
}
rewind(fp);
move(fp);//读取磁盘中的数据转移成链表
p1=head;
printf("\n Please input the Name for deletion:");
scanf("%s",p1->name);
while(strcmp(name,p1->name)!=0&&p1->next!=NULL)//未找到,并且后面还有结点
{
p2=p1;p1=p1->next;
}
if(strcmp(name,p1->name)==0)
{
if(p1==head)head=p1->next;//p1指向首结点,把第二个结点位置赋予head
else
{p2->next=p1->next;//将下一个结点地址赋给前一结点地址
printf("you have deleted:%s",name);
}
j--;
}
else printf("Can't find the record");
save(head);
}
fclose(fp);
printf(" continue delete?(y / n)\n");
while(getchar()!='\n');
scanf("%c",&t);
if(t!='y')m=0;
return(0);
}
}
/******************************************************************************/
我是c的初学者,希望大家多多帮忙~~~~
yuzl32 2008-06-16
  • 打赏
  • 举报
回复
乱七八糟的代码..
baihacker 2008-06-16
  • 打赏
  • 举报
回复
在写函数前用注释
描述清楚输入是什么,输出是什么,动作是什么,出错了怎么办?
还有一些其它备注
baihacker 2008-06-16
  • 打赏
  • 举报
回复
代码比较糟
你至少应该写
插入元素的函数
删除元素的函数
从文件载入
保存到文件

所以在这里只需要:
从文件载入
利用删除元素的函数删除
保存到文件

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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