前言:前面写了两个通讯录,一个是
静态的通讯录,另一个是
动态的通讯录,但每当我们录入信息然后关闭控制台的时候,通讯录的那些信息就自动销毁了,那么我们怎么做才能让通讯录录入的信息保存在一个文件里呢?本篇博客的代码介绍了如何在初始化的时候加载已经存在的联系人信息。
通讯录的功能:
*
1.增加联系人信息
* 2.删除指定联系人信息
* 3.查找联系人信息
* 4.修改指定联系人信息
* 5.显示所有联系人信息
* 6.清空所有联系人
* 7.按名字排序所有联系人
* 2.删除指定联系人信息
* 3.查找联系人信息
* 4.修改指定联系人信息
* 5.显示所有联系人信息
* 6.清空所有联系人
* 7.按名字排序所有联系人
* 8.动态的存放联系人信息
* 9.将录入的联系人信息保存在一个文件里,初始化的时候自动加载
【参考代码】:
contact3.h
#ifndef __CONTACT3_H__
#define __CONTACT3_H__
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define MAX 2
#define FILENAME peoinfo.dat
enum SLECT
{
EXIT,
ADD,
DELE,
SEARCH,
ALERT,
SHOW,
EMPTY,
SORT
};
typedef struct People
{
char name[20];
char sex[10];
int age;
char tele[20];
char addr[30];
}people;
typedef struct Contact
{
people *data;
int count;
int capacity;
}contact, *pcontact;
void Add_people(contact *book);
void Search_people(contact *book);
void Dele_people(contact *book);
void Alert_people(contact *book);
void Show_people(contact *book);
void Sort_people(contact *book);
void Empty_people(contact *book);
void Init_contact(contact *book);
void check_capacity(contact *book);
void Save_contact(contact *book);
void Load_people(contact *book);
#endif //__CONTACT3_H__
contact3.c
#include"contact3.h"
//增加联系人
void Add_people(contact *book)
{
assert(book != NULL);
check_capacity(book);
printf("请输入姓名:");
scanf("%s", book->data[book->count].name);
printf("请输入性别:");
scanf("%s", book->data[book->count].sex);
printf("请输入年龄:");
scanf("%d", &book->data[book->count].age);
printf("请输入电话:");
scanf("%s", book->data[book->count].tele);
printf("请输入地址:");
scanf("%s", book->data[book->count].addr);
book->count++;
printf("添加成功\n");
}
static int find_people(contact *book)
{
char names[20];
printf("请输入姓名:");
scanf("%s", names);
int i = 0;
for (; i < book->count; i++)
{
if (0 == strcmp(book->data[i].name, names))
{
return i;
}
}
return -1;
}
//删除联系人
void Dele_people(contact *book)
{
if (book->count == 0)
{
printf("通讯录已空\n");
return;
}
else
{
int ret = find_people(book);
if (ret < 0)
{
printf("查无此人\n");
return;
}
else
{
int i = ret;
for (; i < book->count - 1; i++)
{
book->data[i] = book->data[i + 1];
}
book->count--;
}
}
}
//修改联系人信息
void Alert_people(contact *book)
{
int ret = find_people(book);
if (ret < 0)
{
printf("查无此人\n");
return;
}
else
{
printf("请输入要修改的姓名:");
scanf("%s", book->data[ret].name);
printf("请输入要修改的性别:");
scanf("%s", book->data[ret].sex);
printf("请输入要修改的年龄:");
scanf("%d", &book->data[ret].age);
printf("请输入要修改的电话:");
scanf("%s", book->data[ret].tele);
printf("请输入要修改的地址:");
scanf("%s", book->data[ret].addr);
printf("修改成功\n");
}
}
//搜索指定联系人
void Search_people(contact *book)
{
int ret = find_people(book);
if (ret < 0)
{
printf("没有查找到该联系人\n");
return;
}
else
{
printf("名字:%s 性别:%s 年龄:%d 电话:%s 地址:%s\n",
book->data[ret].name,
book->data[ret].sex,
book->data[ret].age,
book->data[ret].tele,
book->data[ret].addr);
}
}
//打印所以联系人信息
void Show_people(contact *book)
{
if (book->count == 0)
{
printf("此通讯录为空\n");
return;
}
else
{
int i = 0;
printf("%-5s\t%-5s\t%-3s\t%-12s\t%-12s\n", "名字", "性别", "年龄", "电话", "地址");
for (; i < book->count; i++)
{
printf("%-5s\t%-5s\t%-3d\t%-12s\t%-17s\n",
book->data[i].name,
book->data[i].sex,
book->data[i].age,
book->data[i].tele,
book->data[i].addr);
}
}
}
//初始化的时候加载已存在联系人
void Load_people(contact *book)
{
people tmp;
FILE *pfread = fopen("FILENAME", "r");
if (pfread == NULL)
{
perror("use fopen to read");
exit(EXIT_FAILURE);
}
while (fread(&tmp, sizeof(people), 1, pfread))
{
check_capacity(book);
book->data[book->count] = tmp;
book->count++;
}
fclose(pfread);
pfread = NULL;
}
//初始化通讯录
void Init_contact(contact *book)
{
book->count = 0;
book->data = malloc(MAX*sizeof(people));
if (book->data == NULL)
{
perror("use malloc");
exit(EXIT_FAILURE);
}
memset(book->data, 0, MAX*sizeof(people));
book->capacity = MAX;
Load_people(book);
}
//动态扩容,检查到储存空间不够自动扩容
void check_capacity(contact *book)
{
if (book->count == book->capacity)
{
people *ps = realloc(book->data, ((book->capacity + MAX)*sizeof(people)));
if (ps == NULL)
{
perror("use relloc");
exit(EXIT_FAILURE);
}
else
{
book->data = ps;
book->capacity += MAX;
printf("增容成功\n");
}
}
}
//保存录入通讯录联系人信息
void Save_contact(contact *book)
{
int i = 0;
FILE* pfwrite=fopen("FILENAME", "w");
if (pfwrite == NULL)
{
perror("use fopen to write");
exit(EXIT_FAILURE);
}
for (i = 0; i < book->count; i++)
{
fwrite(book->data + i, sizeof(people), 1, pfwrite);
}
fclose(pfwrite);
pfwrite = NULL;
}
//清空通讯录
void Empty_people(contact *book)
{
Save_contact(book);
free(book->data);
book->data = NULL;
book->capacity = 0;
book->count = 0;
printf("清空成功\n");
}
//按姓名排序通讯录
void Sort_people(contact *book)
{
if (book->count > 1)
{
int i = 0;
for (; i < book->count - 1; i++)
{
int j = 0;
for (; j < book->count - 1 - i; j++)
{
if (strcmp(book->data[j].name, book->data[j + 1].name))
{
people temp = book->data[j];
book->data[j] = book->data[j + 1];
book->data[j + 1] = temp;
}
}
}
}
printf("排序成功\n");
}
test.c
#include"contact3.h"
void menu()
{
printf("********通讯录(3)********\n");
printf("***1.Add 2.Dele****\n");
printf("***3.Search 4.Alter***\n");
printf("***5.Show 6.Empty***\n");
printf("***7.Sort 0.Exit****\n");
printf("*************************\n");
}
int main()
{
int input = 0;
contact book;
Init_contact(&book);
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case ADD:
Add_people(&book);
break;
case DELE:
Dele_people(&book);
break;
case SEARCH:
Search_people(&book);
break;
case ALERT:
Alert_people(&book);
break;
case SHOW:
Show_people(&book);
break;
case EMPTY:
Empty_people(&book);
break;
case SORT:
Sort_people(&book);
break;
default:
printf("输入有误,请重新输入\n");
break;
}
} while (input);
return 0;
}
参考源码:
通讯录3