addr_book.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define NAME_SIZE 200
#define PHONE_SIZE 200
#define ADDR_SIZE 200
#define FILE_PATH "./data.txt"
enum
{
DISPLAY = 1,
ADD = 2,
ERASE = 3,
FIND = 4,
MODIFY = 5,
EXIT = 0
};
typedef struct PersonInfo
{
char name[NAME_SIZE];
char phone[PHONE_SIZE];
char addr[ADDR_SIZE];
} PersonInfo;
typedef struct AddrBook
{
PersonInfo* data;
size_t size;
size_t capacity;
} AddrBook;
void AddrBookInit(AddrBook* addr_book);//初始化通讯录
void AddrBookAdd(AddrBook* addr_book);//通讯录中增加条目
void AddrBookErase(AddrBook* addr_book);//删除通讯录中的指定条目
void AddrBookFind(AddrBook* addr_book);//按指定规则查找通讯录中特定条目
void AddrBookDisplay(AddrBook* addr_book);//打印通讯录中的所有信息
void AddrBookModify(AddrBook* addr_book);//修改通讯录的指定条目
void AddrBookDestroy(AddrBook* addr_book);//销毁通讯录
void AddrBookSave(AddrBook* addr_book);//将通讯录内容保存在文件
void AddrBookLoad(AddrBook* addr_book);//将文件中的内容加载回内存
addr_book.c
#include "addr_book.h"
//初始化通讯录
void AddrBookInit(AddrBook* addr_book)
{
if (addr_book == NULL)
{
printf("addr_book is NULL\n");
return;
}
addr_book->size = 0;
addr_book->capacity = 1;
addr_book->data = (PersonInfo*)malloc(sizeof(PersonInfo)*addr_book->capacity);
}
void AddrBookRelloc(AddrBook* addr_book)
{
if (addr_book == NULL)
{
printf("addr_book is NULL\n");
return;
}
addr_book->capacity = addr_book->capa