修改项目通讯录:利用文件读取数据,写入数据。
头文件:AddressList.h
#ifndef __ADDRESSLIST_H__
#define __ADDRESSLIST_H__
#define FALSE 0
#define TRUE 1
#define N 20
typedef struct _information
{
int id;
char name[N];
char tel[N];
char address[N];
char officetel[N];
}LinkData;
typedef struct _node
{
LinkData data;
struct _node * next;
}Node;
//菜单栏
int Interface_Display();
// 创建链表
Node * Create_List();
//尾插
int Insert_Last(Node *h, LinkData data);
// 添加好友
int Add_Friend(Node *h, int num);
//查找好友
int Find_Friend(Node* h, char *name);
// 删除指定数据
int Delete_Frined(Node *h, char *name);
//通过ID 和 name删除指定数据
int Delete_Id(Node *h, int id, char *name);
//获取链表长度
int Get_Len(Node *h);
//通讯录排序
int Order_Len(Node *h, int *len);
//输出通讯录
int Display(Node *h);
// 写入文件
int write_data(Node *h);
// 读取文件
int read_data(Node *h);
//删除 第 pos 个结点
int Delete_Pos(Node* h, int pos);
// 清空链表
int Clean_List(Node *h);
// 销毁链表
int Destroy(Node *h);
#endif
AddressList.c
#include <stdio.h>
#include <stdlib.h>
#include "AddressList.h"
#include <string.h>
//主菜单
int Interface_Display()
{
system ("clear");
printf ("\t*****************************************\n");
printf ("\t~ 欢迎使用通讯录 ~\n");
printf ("\t~ ~\n");
printf ("\t~ 1 >>>>>>>>> 添加好友信息 ~\n");
printf ("\t~ 2 >>>>>>>>> 列表好友信息 ~\n");
printf ("\t~ 3 >>>>>>>>> 搜索好友 ~\n");
printf ("\t~ 4 >>>>>>>>> 删除好友 ~\n");
printf ("\t~ 5 >>>>>>>>> 退出 ~\n");
printf ("\t~ ~\n");
printf ("\t~ ~\n");
printf ("\t~ ~\n");
printf ("\t~ ~\n");
printf ("\t*****************************************\n");
printf (" \n");
printf ("\t请输入对应数字选择相应功能:");
}
//创建链表
Node * Create_List()
{
Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
if (list == NULL)
return NULL;
list->next = NULL; //空表
return list;
}
//尾插
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;
}
// 添加好友
int Add_Friend(Node *h, int num)
{
if(h == NULL)
return FALSE;
Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
if(node == NULL)
{
return FALSE;
}
system("clear");
node->data.id = num;
printf ("\t好友的ID为:%d\n", node->data.id);
printf ("\n");
printf ("\t请输入好友的姓名:");
scanf ("%s", node->data.name);
printf ("\n");
printf ("\t请输入好友的手机号码:");
scanf ("%s", node->data.tel);
printf ("\n");
printf ("\t请输入好友的家庭地址:");
scanf ("%s", node->data.address);
printf ("\n");
printf ("\t请输入好友的公司电话:");
scanf ("%s", node->data.officetel);
printf ("\n");
node->next = NULL;
printf ("\n");
printf("\t添加成功!\n");
Node *tmp = h;
while(tmp->next)
{
tmp = tmp->next;
}
tmp->next = node;
return TRUE;
}
//通讯录排序
int Order_Len(Node *h, int *len)
{
if (h == NULL)
{
return FALSE;
}
if (h->next == NULL)
{
printf ("通讯录为空\n");
return TRUE;
}
Node *tmp = (Node*)malloc(sizeof(Node)/sizeof(char));
if (tmp == NULL)
{
return FALSE;
}
int i,j;
for(i = 0; i < (*len)-1; i++)
{
Node *min = h->next;
Node *max = h->next->next;
for(j = 0; j < (*len)-i-1; j++)
{
if(min->data.id > max->data.id)
{
tmp->data = min->data;
min->data = max->data;
max->data = tmp->data;
}
min = min->next;
max = max->next;
}
}
free(tmp);
return TRUE;
}
//输出列表好友
int Display(Node *h)
{
if(h == NULL)
return;
Node *tmp = h->next;
while(tmp)
{
printf ("\t%d", tmp->data.id);
printf ("\t%s", tmp->data.name);
printf ("\t%s", tmp->data.tel);
printf ("\t%s", tmp->data.address);
printf ("\t\t%s", tmp->data.officetel);
printf("\n");
tmp = tmp->next;
}
printf("\n");
}
//查找好友
int Find_Friend(Node *h, char *name)
{
if (h == NULL)
return FALSE;
Node *tmp;
int k = 0;
for(tmp = h->next; tmp != NULL; tmp = tmp->next)
{
if (strcmp(tmp->data.name, name) == 0)
{
k++;
printf ("\t查询的好友信息:\n\tID: %d\n \t姓名: %s\n \t手机号码: %s\n \t家庭地址: %s\n \t公司电话: %s\n", tmp->data.id, tmp->data.name, tmp->data.tel, tmp->data.address, tmp->data.officetel);
}
printf ("\n");
}
if (k == 0)
{
printf ("\t您的通讯录中没有该好友!\n");
}
return k;
}
//删除指定数据
int Delete_Frined(Node *h, char *name)
{
if (h == NULL)
return FALSE;
Node *tmp = h;
while (tmp->next)
{
if (strcmp(tmp->next->data.name, name) == 0)
{
break;
}
tmp = tmp->next;
}
if (tmp->next == NULL)
return FALSE;
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
//通过 id 删除指定数据
int Delete_Id(Node *h, int id, char *name)
{
if (h == NULL)
return FALSE;
Node *tmp = h;
while (tmp->next)
{
if (strcmp(tmp->next->data.name, name) == 0 && tmp->next->data.id == id)
{
break;
}
tmp = tmp->next;
}
if (tmp->next == NULL)
return FALSE;
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
//获取链表长度
int Get_Len(Node *h)
{
if (h == NULL)
return 0;
Node *tmp = h;
int count = 0;
while (tmp->next)
{
count++;
tmp = tmp->next;
}
return count;
}
// 写入文件
int write_data(Node *h)
{
if (h == NULL)
return FALSE;
FILE *fp = fopen("friend", "wb");
if (fp == NULL)
{
perror ("fopen");
return;
}
//获取链表中结点的个数
int count_node = Get_Len(h);
// 要写入个数
if (fwrite(&count_node, sizeof(int), 1, fp) != 1)
{
perror ("fwrite");
return FALSE;
}
int i;
Node *tmp = h->next; // 第一个结点
for (i = 0; i < count_node; i++)
{
// 写入数据长度
int len = sizeof(tmp->data);
if (fwrite(&len, sizeof(int), 1, fp) != 1)
{
perror ("fwrite");
return FALSE;
}
// 写入数据
if (fwrite(&(tmp->data), len, 1, fp) != 1)
{
perror ("fwrite");
return FALSE;
}
tmp = tmp->next;
}
fclose(fp);
return TRUE;
}
// 读取文件
int read_data(Node *h)
{
if (h == NULL)
return FALSE;
FILE *fp = fopen("friend", "rb");
if (fp == NULL)
{
perror ("fopen");
return;
}
//读记录的个数
int count_node;
if (fread(&count_node, sizeof(int), 1, fp) != 1)
{
perror ("fread");
return FALSE;
}
int i;
LinkData data;
for (i = 0; i < count_node; i++)
{
int len;
if (fread(&len, sizeof(int), 1, fp) != 1)
{
perror ("fread");
return FALSE;
}
// 读取数据
if (fread(&(data), len, 1, fp) != 1)
{
perror ("fread");
return FALSE;
}
Insert_Last(h, data);
}
fclose(fp);
return TRUE;
}
//删除 第 pos 个结点
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 0;
}
// 销毁链表
int Destroy(Node *h)
{
if (h == NULL)
return FALSE;
Clean_List(h);
free(h);
return TRUE;
}
main.c
#include <stdio.h>
#include "AddressList.h"
#include <stdlib.h>
#include <string.h>
int main()
{
Node* head = Create_List();
if (head == NULL)
{
printf("通讯录创建失败!\n");
return -1;
}
// 读取文件
read_data(head);
char name[20];
int i;
char tmp[N];
int count = 1;
while(1)
{
Interface_Display();
int Function;
scanf ("%d", &Function);
switch(Function)
{
case 1:
{
system ("clear");
Function = 0;
Add_Friend(head, count++);
//write_data(data, x);
printf ("请输入 Enter 返回菜单:");
getchar();
getchar();
break;
}
case 2:
{
system ("clear");
printf("\t*******************好友信息*******************\n");
Function = 0;
//count = Get_Len(head);
//Order_Len(head, &count); // 排序
Display(head);
printf ("请输入 Enter 返回菜单:");
getchar();
getchar();
break;
}
case 3:
{
system ("clear");
printf("\t*******************查找好友*******************\n");
Function = 0;
printf ("请输入需要查找的好友姓名: \n");
scanf ("%s", name);
printf ("\n");
Find_Friend(head, name);
printf ("请输入 Enter 返回菜单:");
getchar();
getchar();
break;
}
case 4:
{
system ("clear");
Function = 0;
printf ("请输入需要删除的好友姓名: ");
scanf ("%s", name);
printf ("\n");
int k = Find_Friend(head, name);
if (k == 0)
{
printf ("需要删除的好友不存在!\n");
}
else if (k == 1)
{
Delete_Frined(head, name);
printf ("好友已删除!\n");
}
else
{
system ("clear");
Find_Friend(head, name);
printf ("请输入要删除的ID号:");
int id;
scanf ("%d", &id);
Delete_Id(head, id, name);
printf ("删除成功!\n");
}
printf ("请输入 Enter 返回菜单:");
getchar();
getchar();
break;
}
case 5:
{
Function = 0;
system ("clear");
// 写入文件
write_data(head);
Destroy(head);
exit(0);
}
default:
printf ("\t操作有误,请重新输入整数1~5\n");
printf ("\t输入回车键。。。。。。。。。");
getchar();
getchar();
break;
}
}
return 0;
}