| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 551 人关注过本帖
标题:[原创]通讯录指针以及文件运用出错
只看楼主 加入收藏
池中月
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-4-10
收藏
 问题点数:0 回复次数:1 
[原创]通讯录指针以及文件运用出错

#include <malloc.h>
#include <string.h>
#include <stdio.h>

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

struct addr
{
char name[20];
char mobilephone[11];
char homephone[11];
struct addr *next;
};

struct addr *creat();
void insert(struct addr *head,FILE *);
struct addr *research(struct addr *head, FILE *fp);
struct addr *dele(struct addr *head,FILE *);
void modify(struct addr *head,FILE *);
void print(struct addr *p);
void face(struct addr *head, FILE *);
void save(struct addr *head, FILE *);
void showall(struct addr *head,FILE *);


int main(int argc, char **argv)
{
struct addr *head;
FILE *fp;

if ( ( fp = fopen("newaddress.txt","r+")) == 0 )
{
printf("file is not exist\n");
printf("create a file\n");
if ((fp = fopen ("newaddress.txt","w+")) == 0)
{
printf("Create file failed\n");
return 1;
}
}

rewind(fp);
if ( fgetc(fp) == -1)
{
printf("\nnow address is empty,please input record\n");
head = creat();
}
face(head,fp);
fclose(fp);
}

struct addr *creat()
{
struct addr *head,*p;
head = NULL;
p = (struct addr *)malloc(LEN);
printf("\ninput name: ");
scanf("%s",p->name);
printf("\ninput mobilephone: ");
scanf("%s",p->mobilephone);
printf("\ninput homephone: ");
scanf("%s",p->homephone);
head = p;
return(head);
}

void face(struct addr *head, FILE *fp)
{
char key;
printf("\ni ------ insert\n");
printf("r ------ research\n");
printf("d ------ delete\n");
printf("m ------ modify\n");
printf("s ------ showall\n");
printf("q ------ quit\n");

while(1)
{
printf("input your choice!");
cscanf("%c",&key);
getch();
switch (key)
{
case 'i':
case 'I':
{
printf("\n\nenter insert\n");
insert(head,fp);
}
break;
case 'r':
case 'R':
{
research(head, fp);
}
break;
case 'd':
case 'D':
{
dele(head,fp);
}
break;
case 'm':
case 'M':
{
modify(head, fp);
}
break;
case 's':
case 'S':
{
showall(head,fp);
}
break;
case 'Q':
case 'q':
{
getch();
save(head, fp);
printf("\n****** save ******\n");
getch();
exit(0);
}
break;
default:printf("\nchoice again!");
}
}
}

void insert(struct addr *head,FILE *fp)
{
struct addr *p,*p1;
p = head;
p1 = (struct addr *)malloc(LEN);
if (head !=NULL)
{
while (p->next !=NULL) /*move p to the last record */
{
p = p->next;
}
printf("input insert record\n");
printf("\tinput name: ");
scanf("%s",p1->name);
printf("\n\tinput mobilephone: ");
scanf("%s",p1->mobilephone);
printf("\n\tinput homephone: ");
scanf("%s",p1->homephone);
p->next = p1;
p=p1;
p->next = NULL;
face(head,fp);
}
else
{
printf("\ninput name: ");
scanf("%s",p1->name);
printf("\ninput mobilephone: ");
scanf("%s",p1->mobilephone);
printf("\ninput homephone: ");
scanf("%s",p1->homephone);
head = p1;
face(head,fp);
}

}


struct addr *research(struct addr *head, FILE *fp)
{
struct addr *p;
int t=0;
char key,who[20],phone[11];
p = head;
printf("\n\t\tresearch in item of name ------ 1");
printf("\n\t\tresearch in item of mobilephone ----- 2");
printf("\n\t\tresearch in item of homephone ----- 3\n");
printf("\ninput research item: ");
cscanf("%c",&key);
getch();
switch (key)
{
case '1':
{
printf("\ninput research name:");
scanf("%s",who);
while (p!=NULL)
{
if ( ( strcmp(p->name,who) ) == 0 )
{
t=1;
break;
}
else
{
p=p->next;
}
}
}
break;
case '2':
{
printf("\ninput research mobilephone:");
scanf("%s",phone);
while (p!=NULL)
{
if ( ( strcmp(p->mobilephone,phone) ) == 0 )
{
t = 1;
break;
}
else
{
p=p->next;
}
}
}
break;
case '3':
{
printf("\ninput research homephone: ");
scanf("%s",phone);
while (p!=NULL)
{
if ( ( strcmp(p->homephone,phone) ) == 0 )
{
t = 1;
break;
}
else
{
p=p->next;
}
}
}
break;
}

if( t == 1)
{
print(p);
getch();

}
else
{
printf("\nrecord is not exist\n");
getch();

}
}

struct addr *dele(struct addr *head,FILE *fp)
{

struct addr *p,*p1;
int t=0;
char key,who[20],phone[11];

/* research record will be delete */
printf("\n\nresearch record will be delete\n");
p1 = p = head;
printf("\n\t\tresearch the record in item of name ------ 1");
printf("\n\t\tresearch the record in item of mobilephone ----- 2");
printf("\n\t\tresearch the record in item of homephone ----- 3\n");
printf("\ninput research item: ");
cscanf("%c",&key);
getch();
switch (key)
{
case '1':
{
printf("\ninput research name:");
scanf("%s",who);
while (p!=NULL)
{
if ( ( strcmp(p->name,who) ) == 0 )
{
t=1;
break;
}
else
{
p1 = p;
p=p->next;
}
}
}
break;
case '2':
{
printf("\ninput research mobilephone:");
scanf("%s",phone);
while (p!=NULL)
{
if ( ( strcmp(p->mobilephone,phone) ) == 0 )
{
t = 1;
break;
}
else
{ p1 = p;
p=p->next;
}
}
}
break;
case '3':
{
printf("\ninput research homephone: ");
scanf("%s",phone);
while (p!=NULL)
{
if ( ( strcmp(p->homephone,phone) ) == 0 )
{
t = 1;
break;
}
else
{ p1 = p ;
p=p->next;
}
}
}
break;
}
if ( t == 1 )
{
print(p);
if ( p = head )
{
head = head->next;
}
else
{
p1->next = p->next;
}
printf("delete succse!\n");

}
else
{
printf("record is not exist\n");
}

getch();
face(head,fp);
}


void modify(struct addr *head,FILE *fp)
{
struct addr *p;
char key;
printf("\nsearch the tecord will be modify in modle research\n");
p = research(head, fp);
printf("\nold record:\n");
print(p);
printf("choice modify item\n");
printf("\n\t\tmodify in item of name ----- 1");
printf("\n\t\tmodify in item of mobilephone ----- 2");
printf("\n\t\tmodify in item of homephone ----- 3\n");
printf("\ninput your choice\n");
cscanf("%c",&key);
getch();
switch (key)
{
case '1':
{
printf("\ninput new name:");
scanf("%s",p->name);
}
break;
case '2':
{
printf("\ninput new mobilephone:");
scanf("%s",p->mobilephone);
}
break;
case '3':
{
printf("\ninput new homephone:");
scanf("%s",p->homephone);
}
break;
default:printf("\nchoice again\n");
}
print(p);
getch();
face(head,fp);
}


void print(struct addr *p)
{
printf("name:%s \tmobilephone:%s \thomephone:%s\n",p->name,p->mobilephone,p->homephone);
}

void showall(struct addr *head, FILE *fp)
{
struct addr *p;
p = head;
/* fp = fopen("newaddress.txt","rt");
rewind(fp); */
printf("\nmodo in showall\n");

while (p != NULL)
{
fread(p,LEN,1,fp);
print(p);
p=p->next;
}
printf("\nshow all ok\n");
fclose(fp);
getch();
face(head,fp);
}


void save(struct addr *head, FILE *fp)
{
/* FILE *fp; */
struct addr *p;
/* fp = fopen("newaddress.txt","a+");
if(fp == NULL)
{
printf("error");
} */
p=head;
while (p != NULL)
{
fwrite(p,LEN,1,fp);
p= p->next;
}
fclose(fp);
}

搜索更多相关主题的帖子: 通讯录 指针 文件 
2006-05-03 15:37
池中月
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-4-10
收藏
得分:0 
请高手指点,谢谢啦


2006-05-03 15:38
快速回复:[原创]通讯录指针以及文件运用出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030229 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved