電話簿(檔案儲存)
阿新 • • 發佈:2018-12-22
相比於動態儲存,用資料夾儲存聯絡人資訊需要改變的有以下幾點:
1.將聯絡人資訊存入檔案
{ FILE* fp = fopen("../test.txt","w"); int i = 0; printf("儲存聯絡人資訊...\n"); if(fp == NULL) { printf("檔案開啟失敗,儲存失敗!\n"); return; } for(i = 0; i<phonebook.size; i++) { fwrite(&phonebook.contact[i], sizeof(Contact), 1, fp); } fclose(fp); printf("儲存成功!共儲存了%d條資訊!\n",phonebook.size); }
2.聯絡人資訊載入
void load() { FILE* fp = fopen("../test.txt","r"); Contact tmp = {0}; printf("載入聯絡人資訊...\n"); if(fp == NULL) { printf("檔案開啟失敗,儲存失敗!\n"); return; } while( fread(&tmp, sizeof(tmp), 1, fp) ) { CheakRealloc(); phonebook.contact[phonebook.size] = tmp; phonebook.size++; } fclose(fp); printf("載入成功!共載入了%d條資訊!\n",phonebook.size); }
3.在主函式中,初始化電話簿後需要載入聯絡人資訊;在每次改動聯絡人資訊後儲存聯絡人資訊:
int main() { int input = 0; Init(); load(); do { menu();//列印選單 printf("請輸入操作序號:"); scanf("%d",&input); clean(); switch(input) { case 1: AddContact(); save(); break; case 2: DelContact(); save(); break; case 3: ModifyContact(); save(); break; case 4: FindContact(); break; case 5: PrintContact(); break; case 6: CleanContact(); save(); break; case 0: printf("歡迎下次使用~~\n"); break; default: printf("輸入序號有誤,請重新輸入!\n"); break; } } while(input); return 0; }
完整程式碼如下:
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "PhoneBook.h"
//列印選單函式
void menu()
{
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃ 歡迎使用電話簿 ┃\n");
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("┃ 1.新增聯絡人 2.刪除聯絡人 ┃\n");
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("┃ 3.修改聯絡人 4.查詢聯絡人 ┃\n");
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("┃ 5.檢視所有聯絡人 6.清空電話簿 ┃\n");
printf("┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
printf("┃ 0.退出電話簿 ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
}
//主函式
int main()
{
int input = 0;
Init();
load();
do
{
menu();//列印選單
printf("請輸入操作序號:");
scanf("%d",&input);
clean();
switch(input)
{
case 1:
AddContact();
save();
break;
case 2:
DelContact();
save();
break;
case 3:
ModifyContact();
save();
break;
case 4:
FindContact();
break;
case 5:
PrintContact();
break;
case 6:
CleanContact();
save();
break;
case 0:
printf("歡迎下次使用~~\n");
break;
default:
printf("輸入序號有誤,請重新輸入!\n");
break;
}
}
while(input);
return 0;
}
phonebook.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "PhoneBook.h"
//清除scanf多餘內容
void clean()
{
char ch = ' ';
while((ch = getchar())!='\n' && (ch != EOF));
}
//將聯絡人資訊存入檔案
void save()
{
FILE* fp = fopen("../test.txt","w");
int i = 0;
printf("儲存聯絡人資訊...\n");
if(fp == NULL)
{
printf("檔案開啟失敗,儲存失敗!\n");
return;
}
for(i = 0; i<phonebook.size; i++)
{
fwrite(&phonebook.contact[i], sizeof(Contact), 1, fp);
}
fclose(fp);
printf("儲存成功!共儲存了%d條資訊!\n",phonebook.size);
}
//聯絡人資訊載入
void load()
{
FILE* fp = fopen("../test.txt","r");
Contact tmp = {0};
printf("載入聯絡人資訊...\n");
if(fp == NULL)
{
printf("檔案開啟失敗,儲存失敗!\n");
return;
}
while( fread(&tmp, sizeof(tmp), 1, fp) )
{
CheakRealloc();
phonebook.contact[phonebook.size] = tmp;
phonebook.size++;
}
fclose(fp);
printf("載入成功!共載入了%d條資訊!\n",phonebook.size);
}
//電話簿排序
void bubble()
{
int i = 0;
int j = 0;
for(i=0; i<phonebook.size; i++)
{
for(j=0; j<phonebook.size-i-1; j++)
{
if(strcmp(phonebook.contact[j].name , phonebook.contact[j+1].name ) > 0)
{
strcpy(phonebook.contact[phonebook.size].name,phonebook.contact[j].name);
strcpy(phonebook.contact[j].name , phonebook.contact[j+1].name);
strcpy(phonebook.contact[j+1].name , phonebook.contact[phonebook.size].name);
strcpy(phonebook.contact[phonebook.size].phone , phonebook.contact[j].phone);
strcpy(phonebook.contact[j].phone , phonebook.contact[j+1].phone);
strcpy(phonebook.contact[j+1].phone , phonebook.contact[phonebook.size].phone);
strcpy(phonebook.contact[phonebook.size].address, phonebook.contact[j].address);
strcpy(phonebook.contact[j].address , phonebook.contact[j+1].address);
strcpy(phonebook.contact[j+1].address , phonebook.contact[phonebook.size].address);
strcpy(phonebook.contact[phonebook.size].age , phonebook.contact[j].age);
strcpy(phonebook.contact[j].age , phonebook.contact[j+1].age);
strcpy(phonebook.contact[j+1].age , phonebook.contact[phonebook.size].age);
strcpy(phonebook.contact[phonebook.size].sex , phonebook.contact[j].sex);
strcpy(phonebook.contact[j].sex , phonebook.contact[j+1].sex);
strcpy(phonebook.contact[j+1].sex , phonebook.contact[phonebook.size].sex);
}
}
}
}
//初始化電話簿
void Init()
{
//int i = 0;
phonebook.size = 0;
phonebook.capacity = 5;
phonebook.contact = (Contact*) malloc(sizeof(Contact) * phonebook.capacity);
//for(i = 0; i < phonebook.capacity; i++)
//{
// strcpy(phonebook.contact[i].name , "");
// strcpy(phonebook.contact[i].phone , "");
//}
memset(phonebook.contact, 0x0, sizeof(Contact));
}
//擴容
void CheakRealloc()
{
if(phonebook.size < phonebook.capacity)
{
return;
}
phonebook.capacity *= 2;
realloc(phonebook.contact , sizeof(Contact) * phonebook.capacity);
}
//新增聯絡人
void AddContact()
{
//if(phonebook.size >= SIZE)
//{
// printf("當前電話簿已滿,新增失敗!");
// return;
//}
CheakRealloc();
printf("開始新增聯絡人\n");
printf("輸入聯絡人姓名:");
scanf("%s",phonebook.contact[phonebook.size].name);
clean();
printf("輸入聯絡人電話:");
scanf("%s",phonebook.contact[phonebook.size].phone);
clean();
printf("輸入聯絡人性別:");
scanf("%s",phonebook.contact[phonebook.size].sex);
clean();
printf("輸入聯絡人年齡:");
scanf("%s",phonebook.contact[phonebook.size].age);
clean();
printf("輸入聯絡人住址:");
scanf("%s",phonebook.contact[phonebook.size].address);
clean();
phonebook.size++;
bubble();
printf("新增聯絡人成功!\n");
}
//刪除聯絡人
void DelContact()
{
int num = 0;
printf("刪除聯絡人!\n");
printf("請輸入你要刪除聯絡人的編號:");
scanf("%d",&num);
clean();
num = num - 1;
if(num < 0 || num >= phonebook.size)
{
printf("輸入編號有誤!\n");
return;
}
if(num != phonebook.size-1)
{
strcpy(phonebook.contact[num].name , phonebook.contact[phonebook.size-1].name);
strcpy(phonebook.contact[num].phone , phonebook.contact[phonebook.size-1].phone);
strcpy(phonebook.contact[num].address , phonebook.contact[phonebook.size-1].address);
strcpy(phonebook.contact[num].sex , phonebook.contact[phonebook.size-1].sex);
strcpy(phonebook.contact[num].age , phonebook.contact[phonebook.size-1].age);
}
phonebook.size--;
bubble();
printf("刪除成功!\n");
}
//修改聯絡人
void ModifyContact()
{
int num = 0;
printf("修改聯絡人!\n");
printf("請輸入你要修改聯絡人的編號:");
scanf("%d",&num);
clean();
if(num < 0 || num >= phonebook.size)
{
printf("輸入編號有誤!\n");
return;
}
printf("請輸入新聯絡人的名字:");
scanf("%s",phonebook.contact[num-1].name);
clean();
printf("輸入聯絡人電話:");
scanf("%s",phonebook.contact[num-1].phone);
clean();
printf("輸入聯絡人性別:");
scanf("%s",phonebook.contact[phonebook.size].sex);
clean();
printf("輸入聯絡人年齡:");
scanf("%s",phonebook.contact[phonebook.size].age);
clean();
printf("輸入聯絡人住址:");
scanf("%s",phonebook.contact[phonebook.size].address);
clean();
bubble();
printf("修改成功!!\n");
}
//查詢聯絡人
void FindContact()
{
char name[1024] = {0};
int i = 0;
int count = 0;
printf("查詢聯絡人電話\n");
printf("輸入聯絡人姓名:");
scanf("%s",name);
for(i=0; i<phonebook.size; i++)
{
if(strcmp(name, phonebook.contact[i].name) == 0)
{
printf("聯絡人的電話為:%s\n性別:%s\n年齡:%s\n住址:%s\n",
phonebook.contact[i].phone,phonebook.contact[i].sex,phonebook.contact[i].age,phonebook.contact[i].address);
printf("----------------------------------------------------------\n");
count++;
}
}
if(count == 0)
{
printf("未找到相關聯絡人!\n");
}
else
{
printf("----------------------------------------------------------\n");
printf("共找到了%d條結果\n",count);
}
}
//列印所有聯絡人
void PrintContact()
{
int i = 0;
bubble();
printf("當前電話簿中儲存的資訊如下:\n");
//printf("(編號) [姓名]-->電話號碼\n");
for(i = 0; i < phonebook.size; i++)
{
printf("(%d) [%s]\n電話:%s\n性別:%s\n年齡:%s\n住址:%s\n",
i+1, phonebook.contact[i].name, phonebook.contact[i].phone,
phonebook.contact[i].sex,phonebook.contact[i].age,phonebook.contact[i].address);
printf("---------------------------------------------------------\n");
}
printf("---------------------------------------------------------\n");
printf("共有%d條資訊\n",i);
}
//清空電話簿
void CleanContact()
{
char input[1024] = {0};
printf("清空所有聯絡人!\n");
printf("是否確定清空所有聯絡人?(y/n)\n");
scanf("%s",input);
if(strcmp(input , "y") != 0)
{
printf("已取消刪除操作!\n");
return;
}
else
phonebook.size = 0;
printf("成功清空電話簿!\n");
}
phonebook.h
#ifndef __PhoneBook_h__
#define __PhoneBook_h__
typedef struct Contact
{
char name[1024];//姓名
char phone[1024];//電話
char address[1024];//住址
char sex[8];//性別
char age[8];//年齡
}Contact;
typedef struct PhoneBook
{
Contact* contact;
int capacity; //最大容量
int size; //當前有效元素
}PhoneBook;
PhoneBook phonebook;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void clean();
void Init();
void AddContact();
void DelContact();
void ModifyContact();
void FindContact();
void PrintContact();
void CleanContact();
void bubble();
void CheakRealloc();
void save();
void load();
#endif //__PhoneBook_h__