将链表所写通讯录存入文件保存

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

struct person				//定义结构体,存储联系人信息
{
    char name[20];
    char tel[12];
    char addr[50];
    struct person *next;
};
struct people
{
	char name[20];
    char tel[12];
    char addr[50];
};

typedef struct person Person;
typedef struct person * Link;
typedef struct people People;
typedef struct people * PLink;

void release(Link* head)					//清空通讯录(释放头结点以外的所有结点)
{
	Link p;
	while((*head)->next != NULL)
	{
		p = (*head)->next;
		(*head)->next = p->next;
		free(p);
	}
}

int create_node(Link * newnode)				//创立结点
{
	int count = 0;
	do										//为避免空间申请失败,进行10次申请,成功即返回
	{
		*newnode = (Link)malloc(sizeof(Person));
		count++;
		if(count == 10)
		{
			return 0;
		}
	}while(!*newnode);
	return -1;	
}
void  createlink(Link * head)				//创建带表头结点的链表
{
	if(!create_node(head))
	{
		release(head);
		printf("创建失败!");
		exit(-1);
	}
	(*head)->next = NULL;
}

void add(Link head, Link newnode)			//添加联系人
{
	if(!create_node(&newnode))
	{
		free(newnode);
		printf("add failed space not enough!\n");
		exit(-1);
	}
	else
	{
		Link p = head->next;
		newnode->next = head->next;
		head->next = newnode;
		scanf("%s %s %s",newnode->name, newnode->tel, newnode->addr);	//添加联系人信息
		while(p != NULL)
		{
			if(strcmp(newnode->name,p->name) == 0)
			{
				printf("联系人已存在\n");
				return;
			}
			p = p->next;
		}
		
		while(strlen(newnode->tel) != 11)
		{
			printf("please enter 11 telphone number again!\n");			//11位联系电话
			scanf("%s",newnode->tel);
		}
		newnode->tel[11] = '\0';
	}
}

void delete(Link head)			//删除联系人
{
	char name[20];
	gets(name);

	Link p, q;
	p = head->next;
	q = head;
	
	while(p != NULL)
	{
		if(strcmp(p->name,name) == 0 )
		{
			q->next = p->next;
			free(p);
			return;
		}
		q = p;
		p = p->next;
	}
	if(p == NULL)
	{
		printf("no such person!\n");
		return;
	}
	
}

void find(Link head)			//查找联系人
{
	char name[20];
	gets(name);
	Link p;
	p = head->next;

	while(p != NULL)
	{
		if(strcmp(p->name,name) == 0)
		{
			printf("%s %s %s\n",p->name, p->tel, p->addr);
		}
		return;
	}
	if(p == NULL)
	{
		printf("no such person!\n");
	}
}

void modify(Link head)			//修改联系人
{
	char name[20];
	gets(name);
	Link p;
	p = head->next;
	while(p != NULL)
	{
		if(strcmp(p->name,name) == 0)
		{
			printf("请重新输入相关信息:");
			scanf("%s %s %s",p->name,p->tel,p->addr);
			getchar();
			return;
		}
		p = p->next;
	}
	if(p = NULL)
	{
		printf("no such person!\n");
	}
	
}

int length(Link head)		//计算联系人个数
{
	int count = 0;
	Link p;
	p = head->next;
	while(p != NULL)
	{
		count++;
		p = p->next;
	}
	return count;
}

void sort(Link head)		//根据姓名大小,用冒泡法排序
{
	Link p,q;
	int len = length(head);
	int i, j;
	char tname[20];
	char ttel[12];
	char taddr[50];
	for(i = len - 2; i >= 0; i--)
	{
		p = head->next;
		q = p->next;
		
		for(j = 0; j <= i; j++)
		{
			if(strcmp(p->name,q->name) > 0)
			{
				strcpy(tname,p->name);
				strcpy(ttel,p->tel);
				strcpy(taddr,p->addr);
				
				strcpy(p->name,q->name);
				strcpy(p->tel,q->tel);
				strcpy(p->addr,q->addr);
				
				strcpy(q->name,tname);
				strcpy(q->tel,ttel);
				strcpy(q->addr,taddr);
			}
			p = p->next;
			q = q->next;
		}
	}
}

void display(Link head)					//显示排序后的所有联系人信息
{
	sort(head);
	Link p;
	p = head->next;
	if(p == NULL)
	{
		printf("no anyone!\n");
	}
	else
	{
		while(p != NULL)
		{
			printf("%s %s %s\n",p->name, p->tel, p->addr);
			p = p->next;
		}
	}
}

void menu()				//功能菜单
{
	printf("\n");
	printf("***** Welcome To Used My Addressbook *****\n");
    printf("***** 1.添加联系人 *****\n");
	printf("***** 2.删除联系人 *****\n");
	printf("***** 3.查找联系人 *****\n");
	printf("***** 4.修改联系人 *****\n");
	printf("***** 5.显示所有联系人 *****\n");
	printf("***** 6.删除所有联系人 *****\n");
	printf("***** 7.退出通讯录 *****\n");
	printf("***** 键入相应值选择对应功能 *****\n");
	printf("***** Welcome To Used My Addressbook *****\n");
	printf("\n");
}

void function(char ch[], Link head, Link newnode)		//各选项对应的功能调用
{
	if(strcmp(ch,"1") == 0)
	{
		printf("enter name tel addr:\n");
		add(head,newnode);
	}
	else if(strcmp(ch,"2") == 0 )
	{
		printf("please enter the name of you need to delete:");
		delete(head);		
	}
	else if(strcmp(ch,"3") == 0)
	{
		printf("please enter the name of you need to find:");
		find(head);
	}
	else if(strcmp(ch,"4") == 0)
	{
		printf("please enter the name of you need to modify:");
		modify(head);
	}
	else if(strcmp(ch,"5") == 0)
	{
		display(head);
	}
	else if(strcmp(ch,"6") == 0)
	{
		release(&head);
	}	
}

void read_to_link(Link head)
{
	int i, j = 0, num, ret;
	Link p, q;
	p = head;
	q = NULL;
	FILE * fp;
	fp = fopen("./communicate.txt","r");
	if(fp == NULL)
	{
		printf("fopen failed errno: %d\n",errno);
		perror("fopen:");
		return;
	}
	fseek(fp,0,SEEK_END);
	i = ftell(fp);
	fseek(fp,0,SEEK_SET);
	num = i / sizeof(People); //获取联系人个数
	People str[100];
	ret = fread(str,sizeof(People),num,fp);
	if(!ret)
	{
		printf("fread failed errno: %d\n",errno);
		perror("fread:");
		return;
	}
	while(num-- > 0)
	{
		if(!create_node(&q))
		{
			free(q);
			printf("create node failed\n");
			exit(-1);
		}
		strcpy(q->name, str[j].name);
		strcpy(q->tel, str[j].tel);
		strcpy(q->addr, str[j].addr);
		p->next = q;
		q->next = NULL;
		p = q;
		j++;
	}
	fclose(fp);

}

void write_to_file(Link head, int len)
{
	People str[100];
	Link p, q;
	p = head->next;
	int i = 0, ret;
	FILE *fp;
	fp = fopen("./communicate.txt","w");
	if(fp == NULL)
	{
		printf("fopen failed errno: %d\n",errno);
		perror("fopen:");
		return;
	}
	while(p != NULL)
	{
		strcpy(str[i].name,p->name);
		strcpy(str[i].tel,p->tel);
		strcpy(str[i].addr,p->addr);
		ret = fwrite(&str[i],sizeof(People),1,fp);
		if(!ret)
		{
			printf("fwrite failed errno: %d\n",errno);
			perror("fwrite:");
			return;
		}
		p = p->next;
		i++;
	}
	fclose(fp);
}

int main()
{
	char ch[2] ;
	Link head = NULL;
	Link newnode = NULL;
	
	createlink(&head);

	read_to_link(head);
	
	while(1)
	{
    	menu();
		scanf("%s",ch);
		getchar();
		ch[1] = '\0';
		
		function(ch, head, newnode);
		
		if(strcmp(ch,"7") == 0)
		{	
			write_to_file(head,length(head));
			release(&head);
			exit(0);
		} 
		if(strcmp(ch,"1") < 0 || strcmp(ch,"7") > 0) 
		{
			printf("输入值非法,请重新输入\n");
		}
	}

    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值