用文件操作实现的通讯录

学习完文件操作这一部分之后就可以对之前做的通讯录进行修改,上一版本的通讯录可以参考我之前的一篇点击打开链接

通过文件操作可以实现将好友信息保存到一个文件中去,从而在下一次运行这个程序可以从这个文件中读取我之前修改或添加过的好友信息;

相比较上一版本,说简单点,就是我添加好友后退出通讯录后,下次再运行通讯录就能看到我之前添加进去的。


LinkList.h文件:

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#define TRUE   1
#define FALSE -1
#define SIZE  50
typedef struct _people
{
	int id;
	char name[SIZE];
	char tel[SIZE];
	char address[SIZE];
	char ctel[SIZE];
}LinkData;

typedef struct _node
{
	LinkData data;
	struct _node *next;
}Node;
int Quit();

void read_data(Node *h);

void write_data(Node *h);

int Sort_List(Node *h);

void Choice(Node *h);

void Search(Node *h);

void Add(Node *h);

void Delete(Node *h);

Node *Find_Element2(Node *h, LinkData d, int *x);

int Delete_Id(Node *h, int d);

int Find_nElement(Node *h, LinkData d);

void Display_Pos(Node *h, int x);

int Fun(int x, Node *h);

void MainInterface();

int Init(Node *h);

Node *Create_List();

void Display_List(Node *h);

int Insert_Last(Node *h, LinkData data);

int Insert_Head(Node *h,LinkData data);

int Insert_Pos(Node *h, int pos, LinkData data);

int Delete_Pos(Node *h, int pos);

int Delete_Data(Node *h, LinkData data);

int Find_Element(Node *h, LinkData d, int *x);

int Get_Element(Node *h, int pos, LinkData *x);

int Reverse_List(Node *h);

int Get_Len(Node *h);

int Clean_List(Node *h);

int Destroy(Node *h);

#endif


LinkList.c文件:

#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>

Node *Create_List()
{
	Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
	if(node == NULL)
		return NULL;
	node->next = NULL;
	return node;
}

int Fun(int x,Node *h)
{	
    MainInterface();
	switch(x)
	{
		case 1:				
				Add(h);
				break;
		case 2:			
				Sort_List(h);
		        Display_List(h);
				break;
		case 3:				
				Search(h);
				break;
		case 4:
				Delete(h);
				break;
		case 5:
				Clean_List(h);
				printf("清除成功!\n");
				break;
		case 6:
				write_data(h);
				Destroy(h);
				system("clear");
				return FALSE;			
		default:
				printf("输入错误,请重新输入\n");
				break;
		
	}
	return TRUE;
}


int Sort_List(Node *h)
{
	if(h == NULL || h->next == NULL || h->next->next == NULL)
		return FALSE;
	int n = Get_Len(h);
	int i,j;
	for(i=0; i<n-1; i++)
	{
		Node *pre = h->next;
		Node *cur = h->next->next;
		Node *tmp = h;
		for(j=0; j<n-1-i; j++)
		{
			if(pre->data.id > cur->data.id)
			{
				pre->next = cur->next;
				cur->next = pre;
				tmp->next = cur;
				
				tmp = cur;
				cur = pre->next;
			}
			else
			{
				tmp = pre;
				pre = cur;
				cur = cur->next;
			}
		}
	}
	return TRUE;
	
}

void write_data(Node *h)          //主要就是write_data()和read_data()这两个函数,这个函数功能是将好友链表信息写入到文件中去

{

	FILE *fp = fopen("people", "wb+");   //这个函数放在退出通讯录之前调用
	if(fp == NULL)
	{
		perror("fopen");
		return;
	}
	int len = Get_Len(h);
	//printf("%d\n",len);
	fwrite(&len, sizeof(int), 1, fp);   //写入链表的长度,就是有多少个好友
	int i;
	Node *tmp = h->next;
	for(i=0; i<len; i++)
	{
		int n = sizeof(tmp->data);
		fwrite(&n, sizeof(int), 1, fp);     //写入一个好友所占空间大小
		fwrite(&(tmp->data), sizeof(LinkData), 1, fp);//写入好友具体信息
		tmp = tmp->next;
	}
	fclose(fp);
}

void read_data(Node *h)      //从文件中读取好友信息,这个函数放在程序开始前调用
{
	if(h == NULL)
		return;
	FILE *fp = fopen("people", "ab+");
	if(fp == NULL)
	{
		perror("fopen");
		return;
	}
	int len,i,n;
	LinkData tmp;
	fread(&n, sizeof(int), 1, fp);  //读取总共多少好友
	//printf("%d\n",n);
	for(i=0; i<n; i++)
	{
		fread(&len, sizeof(int), 1, fp);  //读取一个好友的长度
		fread(&tmp, len, 1, fp);
		Insert_Last(h,tmp);              //读取一个好友的具体信息
	}
	fclose(fp);
}


int Init(Node *h)
{
	if(h == NULL)
		return FALSE;
	LinkData p1 = {5, "liming", "13151587968", "南京市江宁区弘景大道66号", "025-543649"};
	LinkData p2 = {4, "liyang", "13151585173", "上海市宝山区江阳大道67号", "025-785241"};
	LinkData p3 = {3, "liyang", "13151584563", "南通市如东县光明大道68号", "025-555633"};
	LinkData p4 = {2, "liyang", "13151587758", "无锡市富贵区明天大道70号", "025-123789"};
	LinkData p5 = {1, "zhangsan", "13151581238", "南京市江宁区弘景大道45号", "025-542574"};
	
	Insert_Last(h,p1);
	Insert_Last(h,p2);
	Insert_Last(h,p3);
	Insert_Last(h,p4);
	Insert_Last(h,p5);
	
	return TRUE;
}	
int Insert_Last(Node *h, LinkData data)
{
	if(h == NULL)
		return FALSE;
	Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
	if(node == NULL)
		return FALSE;
	node->data = data;
	node->next = NULL;
	Node *tmp = h;
	while(tmp->next)
	{
		tmp = tmp->next;
	}
	tmp->next = node;
	return TRUE;
}


void Display_List(Node *h)
{
	if(h == NULL)
		return;
	int count = 0;
	Node *tmp = h->next;
	if(tmp == NULL)
		printf("\t当前列表内没有好友\n");
	while(tmp)
	{
		printf("\tid: %d                     姓名:%s\n",tmp->data.id,tmp->data.name);
		printf("\t个人电话:%11s     家庭住址:%s\n",tmp->data.tel,tmp->data.address);
		printf("\t公司电话:%11s\n",tmp->data.ctel);
		printf("\n");
		//printf("\t%d,%s,%s,%s,%s\n",tmp->data.id,tmp->data.name,tmp->data.tel,tmp->data.address,tmp->data.ctel);
		tmp = tmp->next;
	}
	printf("\n");
}

void MainInterface()
{
	system("clear");
	printf("\t***************************通讯录*****************************\n");
	printf("\t*                                                            *\n");
	printf("\t*    1.添加好友信息                   2.列表好友信息         *\n");
	printf("\t*                                                            *\n");
	printf("\t*                                                            *\n");
	printf("\t*    3.搜索好友信息                  4.删除好友信息          *\n");
	printf("\t*                                                            *\n");
	printf("\t*                                                            *\n");
	printf("\t*    5.清除好友列表                  6.退出                  *\n");
	printf("\t*                                                            *\n");
	printf("\t*                                                            *\n");
	printf("\t***************************通讯录*****************************\n");
	printf("  \t请输入指令:\n");
	return;
}


int Find_Element(Node *h, LinkData d, int *x)
{
	if(h == NULL)
		return FALSE;
	int count = 0;
	Node *tmp = h->next;
	while(tmp)
	{
		count++;
		if(strcmp(tmp->data.name ,d.name) == 0)
			break;
		tmp = tmp->next;
	}
	if(tmp == NULL)
	{
		printf("没有该好友\n");
		return FALSE;
	}
	else
	{
		*x = count;
		return TRUE;
	}
}

Node *Find_Element2(Node *h, LinkData d, int *x)
{
	if(h == NULL)
		return NULL;
	int count = 0;
	Node *tmp = h->next;
	while(tmp)
	{
		count++;
		if(strcmp(tmp->data.name ,d.name) == 0)
			break;
		tmp = tmp->next;
	}
	if(tmp == NULL)
	{
		printf("没有该好友\n");
		return NULL;
	}
	else
	{
		*x = count;
		return tmp;
	}
}

int Find_nElement(Node *h, LinkData d)
{
	if(h == NULL)
		return FALSE;
	int count = 0;
	Node *tmp = h->next;
	while(tmp)
	{
		if(strcmp(tmp->data.name, d.name) == 0)
			count++;
		tmp = tmp->next;
	}
	return count;

}

int Delete_Pos(Node *h, int pos)
{
	if(h == NULL || pos < 1)
		return FALSE;
	Node *tmp = h;
	int i;
	for(i=0; i<pos-1; i++)
	{
		if(tmp->next == NULL)
			break;
		tmp = tmp->next;
	}
	if(tmp->next == NULL)
	{
		printf("删除越界\n");
		return FALSE;
	}
	Node *p = tmp->next;
	tmp->next = p->next;
	free(p);
	return TRUE;
}
int Clean_List(Node *h)
{
	if(h == NULL)
		return FALSE;
	Node *tmp = h;
	while(tmp->next)
	{
		Delete_Pos(h,1);
	}
	return TRUE;
}

int Delete_Id(Node *h, int d)
{
	if(h == NULL)
		return FALSE;
	
	Node *tmp = h;
	while(tmp->next)
	{
		if(tmp->next->data.id == d)
			break;
		tmp = tmp->next;
	}
	if(tmp->next == NULL)
	{
		printf("没有该好友\n");
		return FALSE;
	}
	else
	{
		Node *p = tmp->next;
		tmp->next = p->next;
		free(p);
		return TRUE;
	}
}


void Display_Pos(Node *h, int x)
{
	if(h == NULL)
		return;
	int i;
	Node *tmp = h->next;
	for(i=0; i<x-1; i++)
	{
		tmp = tmp->next;
	}
	printf("\tid: %d                     姓名:%s\n",tmp->data.id,tmp->data.name);
	printf("\t个人电话:%s     家庭住址:%s\n",tmp->data.tel,tmp->data.address);
	printf("\t公司电话:%s\n",tmp->data.ctel);
	printf("\n");
}

void Add(Node *h)
{
	printf("\t请输入好友信息:\n");
	LinkData data;
	printf("\t请输入id:\n");
	scanf("%d",&data.id);
	printf("\t请输入姓名:\n");
	scanf("%s",data.name);
	printf("\t请输入电话号码:\n");
	scanf("%s",data.tel);
	printf("\t请输入家庭地址:\n");
	scanf("%s",data.address);
	printf("\t请输入公司号码:\n");
	scanf("%s",data.ctel);
	if(Insert_Last(h,data) == TRUE)
	{
		printf("\t添加成功\n");
	}
	else
		printf("\t添加失败\n");
	
	return;
}


void Search(Node *h)
{
	printf("\t请输入要查找好友的姓名:\n");
	LinkData d1;
	int a1 = -3;
	scanf("%s",d1.name);
	if(Find_Element(h, d1, &a1) == TRUE)
	{
		//Display_Pos(h,a1);
		if(Find_nElement(h, d1) == 1)
		{
			Display_Pos(h,a1);
		}
		else
		{
			int j;
			int n = Find_nElement(h, d1);
			
			Node *p = h;
			for(j=0; j<n; j++)
			{
				Node *q=Find_Element2(p, d1, &a1);
				Display_Pos(p, a1);
				p = q;
			}
		}
	}
	return;
}

void Delete(Node *h)
{
	printf("\t请输入要删除的好友姓名:\n");
	LinkData d2;
	int a2 = -2;
	scanf("%s",d2.name);
	if(Find_Element(h, d2, &a2) == TRUE)
	{
		if(Find_nElement(h, d2) == 1)
		{
			Find_Element(h, d2, &a2);
			Delete_Pos(h,a2);
			printf("删除成功\n");
		}
		else
		{
			int j,id;
			int n = Find_nElement(h, d2);
			
			Node *p = h;
			for(j=0; j<n; j++)
			{
				Node *q=Find_Element2(p, d2, &a2);
				Display_Pos(p, a2);
				p = q;
			}
			printf("\t请输入id: \n");
			scanf("%d",&id);
			if(Delete_Id(h,id) == TRUE)
				printf("\t删除成功\n");
		}
	}
	return;
}

int Get_Len(Node *h)
{
	if(h == NULL)
		return FALSE;
	int count = 0;
	Node *tmp = h;
	while(tmp->next)
	{
		count++;
		tmp = tmp->next;
	}
	return count;
}

int Quit()
{
	return FALSE;
}


int Destroy(Node *h)
{
	if(h == NULL)
		return FALSE;
	Clean_List(h);
	free(h);
	return TRUE;
}



main.c文件:

#include "LinkList.h"
#include <stdio.h>

int main()
{
	Node *head = Create_List();
	if(head == NULL)
	{
		printf("\t创建链表失败\n");
		return -1;
	}
	
	
	/*
	int i;
	LinkData a[5] = {
		{5, "liming", "13151587968", "南京市江宁区弘景大道66号", "025-543649"},
		{4, "liyang", "13151585173", "上海市宝山区江阳大道67号", "025-785241"},
		{3, "suyang", "13151584563", "南通市如东县光明大道68号", "025-555633"},
		{2, "liyang", "13151587758", "无锡市富贵区明天大道70号", "025-123789"},
		{1, "zhangsan", "13151581238", "南京市江宁区弘景大道45号", "025-542574"},
	};
	int len = sizeof(a)/sizeof(a[0]);
	write_data(a,len);
	*/
	read_data(head);
	
	
	MainInterface();
	
	while(1)
	{
		int x;
		scanf("%d",&x);
		if(Fun(x,head) == FALSE)
			break;
		//Display_List(head);
	}
	
	
	return 0;
}


相比较上一个版本,这个版本就是多写了两个函数,读和写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值