上次做完通讯录后,总是有点遗憾,就是没有把电话数据保存到本地,没有实质上的实现通讯录功能,今天,我把通讯录重新拿出来做了一次,对它进行文件保存。
做完之后,感觉其实很简单,就是在数据存入的时候,不要再添加结点到链表中了,直接把这个结构体数据保存到文件中,查看的时候就是从文件中把数据读出来,这个时候需要用到链表了,因为不知道文件中数据的数量,所以链表是一个不错的选择。读出来之后,再把链表打印一下。稍微麻烦一点的就是删除了,先把数据读出来,然后进行遍历删除,接着把原文件删除,然后再把链表一个一个存进文件。
/**********************************************************
File Name: 通讯录项目(文件保存)
Author: Date:2017-2-6
Description: 实现通讯录功能
Fuction List: show() 显示最初布局
Init_list() 添加好友信息
Print() 列表好友信息
Search() 搜索好友
Delete() 删除好友
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define ok 0
#define error -1
#define malloc_error -2
#define N 20
typedef struct node
{
char ID;
char name[N];
char tel[N ];
char address[N];
char company_phone[N];
struct node *next;
}Address;
typedef Address* PAddress;
int i = 1;
//显示最初布局
void show()
{
system("clear");
printf("\t\t*********************************************\n");
printf("\t\t* \twelcome to address book *\n");
printf("\t\t* *\n");
printf("\t\t*\tA) Add the information of friends *\n");
printf("\t\t*\tB) Show all friends information *\n");
printf("\t\t*\tC) Search for the friend *\n");
printf("\t\t*\tD) Remove the friend *\n");
printf("\t\t* *\n");
printf("\t\t*********************************************\n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" \n");
printf(" please input your choice:");
}
//添加好友信息
int Init_list(PAddress h)
{
if(h == NULL)
{
return error;
}
PAddress p = (PAddress)malloc(sizeof(Address)/sizeof(char));
if (p == NULL)
{
return malloc_error;
}
system("clear");
p->ID = '0'+i;
i++;
printf("\n please input the name:");
scanf("%s", p->name); //输入姓名
printf("\n please input the tel:");
scanf("%s", p->tel); //输入手机号
int j = 0;
int len = 0;
while(p->tel[j++] != '\0')
{
len++;
}
while(len != 11) //判断输入的手机号码是不是11位的
{
printf("\n please input 11 numbers,input again:");
scanf("%s", p->tel);
len = 0;
j = 0;
while(p->tel[j++] != '\0')
{
len++;
}
}
printf("\n please input the address:");
scanf("%s", p->address);
printf("\n please input the company phone:");
scanf("%s", p->company_phone);
j = 0;
len = 0;
while(p->company_phone[j++] != '\0')
{
len++;
}
while(len != 8) //判断输入的电话号码是不是8位的
{
printf("\n please input 8 numbers,input again:");
scanf("%s", p->company_phone);
len = 0;
j = 0;
while(p->company_phone[j++] != '\0')
{
len++;
}
}
p->next = NULL;
int fd;
int ret;
fd = open("address_book.txt", O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);
if(fd == -1)
{
perror("open");
return;
}
ret = write(fd, p, sizeof(Address));
if(ret == -1)
{
perror("write");
return;
}
close(fd);
return ok;
}
//列表好友信息
void Print()
{
int fd;
int ret;
fd = open("address_book.txt", O_RDONLY, S_IRUSR | S_IWUSR);
if(fd == -1)
{
printf("There is no address_book.txt!\n");
return;
}
PAddress h = (PAddress)malloc(sizeof(Address)/sizeof(char));
PAddress p = (PAddress)malloc(sizeof(Address)/sizeof(char));
ret = read(fd, p, sizeof(Address));
if(ret == 0)
{
printf("There is no friend!\n");
return;
}
h->next = p;
while(1)
{
PAddress t = (PAddress)malloc(sizeof(Address)/sizeof(char));
ret = read(fd, t, sizeof(Address));
if(ret == 0)
{
p->next = NULL;
break;
}
p->next = t;
p = p->next;
}
PAddress temp = h->next;
while(temp)
{
printf("Num:%c name:%s tel:%s address:%s company phone:%s\n",
temp->ID , temp->name, temp->tel, temp->address, temp->company_phone);
temp = temp->next;
}
printf("\n");
close(fd);
}
//搜索好友
int Search(int i)
{
int fd;
int ret;
int flag = 1;
fd = open("address_book.txt", O_RDONLY, S_IRUSR | S_IWUSR);
if(fd == -1)
{
printf("There is no address_book.txt!\n");
return;
}
PAddress h = (PAddress)malloc(sizeof(Address)/sizeof(char));
PAddress p = (PAddress)malloc(sizeof(Address)/sizeof(char));
ret = read(fd, p, sizeof(Address));
if(ret == 0)
{
printf("There is no friend!\n");
return;
}
h->next = p;
while(1)
{
PAddress t = (PAddress)malloc(sizeof(Address)/sizeof(char));
ret = read(fd, t, sizeof(Address));
if(ret == 0)
{
p->next = NULL;
break;
}
p->next = t;
p = p->next;
}
PAddress temp = h->next;
while(temp)
{
if(temp->ID == i)
{
flag = 0;
printf("\n\n");
printf("Num:%c name:%s tel:%s address:%s company phone:%s\n",
temp->ID, temp->name, temp->tel, temp->address, temp->company_phone);
printf("\n\n");
}
temp = temp->next;
}
if(flag)
{
printf("\n\n");
printf("Don't have the friend\n");
printf("\n\n");
}
return ok;
}
//删除好友
int Delete(int i)
{
int fd;
int ret;
fd = open("address_book.txt", O_RDONLY, S_IRUSR | S_IWUSR);
if(fd == -1)
{
printf("There is no address_book.txt!\n");
return;
}
PAddress h = (PAddress)malloc(sizeof(Address)/sizeof(char));
PAddress p = (PAddress)malloc(sizeof(Address)/sizeof(char));
ret = read(fd, p, sizeof(Address));
if(ret == 0)
{
printf("There is no friend!\n");
return;
}
h->next = p;
while(1)
{
PAddress t = (PAddress)malloc(sizeof(Address)/sizeof(char));
ret = read(fd, t, sizeof(Address));
if(ret == 0)
{
p->next = NULL;
break;
}
p->next = t;
p = p->next;
}
close(fd);
PAddress temp = h;
if(temp->next->next == NULL) //一个结点的表
{
if(temp->next->ID == i)
{
PAddress tmp = temp->next;
temp->next = NULL;
free(tmp);
ret = remove("address_book.txt");
if(ret == -1)
{
perror("remove");
return;
}
int fd2 = open("address_book.txt", O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);
close(fd2);
printf("\n\n");
printf("Deleted successfully\n");
printf("\n\n");
return ok;
}
else
{
return error;
}
}
while(temp) // 此处temp 不能写temp->next 要考虑删除的是最后一个结点
{
if(temp->next->ID == i)
{
PAddress p = temp->next;
temp->next = p->next;
free(p);
printf("\n\n");
printf("Deleted successfully\n");
printf("\n\n");
break;
}
if(temp->next->next == NULL ) //考虑i超出了表中元素的时候
{
return error;
}
temp = temp->next;
}
int fd1;
fd1 = open("address_book.txt", O_RDWR | O_TRUNC | O_APPEND, S_IRUSR | S_IWUSR);
temp = h->next;
while(temp)
{
ret = write(fd1, temp, sizeof(Address));
if(ret == -1)
{
perror("write");
return;
}
temp = temp->next;
}
close(fd1);
return ok;
}
int main()
{
char option[2]; //定义一个字符串存放输入的选项 去除回车键对循环的影响
int back = 1;
char id[3];
PAddress head_node = (PAddress)malloc(sizeof(Address)/sizeof(char)); //定义头结点
if(head_node == NULL)
{
return malloc_error;
}
head_node->next = NULL;
while(back--)
{
show();
scanf("%s", option);
switch (option[0])
{
case 'A' :
{
Init_list(head_node);
system("clear");
printf("\n\n\n\n\n\n");
printf(" Congratulations on your success to create contacts!\n");
printf("\n\n\n\n\n\n");
printf(" If you want to back,please input (1):");
scanf("%d", &back);
break;
}
case 'B' :
{
back = 0;
system("clear");
printf("\n\n\n\n\n\n");
Print();
printf("If you want to back,please input (1):");
scanf("%d", &back);
break;
}
case 'C' :
{
back = 0;
system("clear");
printf("\n\n\n\n\n\n");
printf("if you want to search your friend,please input ID:");
scanf("%s",id);
Search(id[0]);
printf("If you want to back,please input (1):");
scanf("%d", &back);
break;
}
case 'D' :
{
back = 0;
system("clear");
printf("\n\n\n\n\n\n");
printf("if you want to delete your friend,please input ID:");
scanf("%s",id);
if (Delete(id[0]) != ok)
{
printf("\n\n");
printf("num %d friend is not exist!", id);
printf("\n\n");
}
printf("If you want to back,please input (1):");
scanf("%d", &back);
break;
}
default :
{
printf("\n\n");
system("clear");
printf("\n\n\n\n\n\n");
printf("\t\tplaese input A、B、C or D!\n");
printf("\n\n");
printf("If you want to back,please input (1):");
scanf("%d", &back);
break;
}
}
}
return 0;
}