通讯录的文件版本
1. 需求
在学习了文件操作之后 我们学会了如何读写文件
现在产品经理跑了过来 对我们说 通讯录的功能还不是太完善
要求我们增加一个持久化保存的功能
2. 实现
我们先想想 要做到一个持久化的通讯录 我们首先要做到保存数据
打开我们的文件
int main() { struct contact c1; inicontact(&c1); int input = 1; do { menu(); scanf("%d", &input); switch (input) { case 1: system("cls"); addpeople(&c1); break; case 2: system("cls"); delpeople(&c1); break; case 3: system("cls"); srhpeople(&c1); break; case 4: system("cls"); revpeople(&c1); break; case 5: system("cls"); showpeople(&c1); break; case 6: system("cls"); inicontact(&c1); break; case 7: system("cls"); sortpeople(&c1); break; case 0: system("cls"); killpeople(&c1); break; default: break; } } while (input); return 0;
在我们的第 case 0中 销毁通讯录之前 我们先创建一个文件保存数据
我们规定 保存文件的函数叫做 speople()
代码表示如下
void speople(struct contact* pc) { FILE* pf = fopen("test.txt", "rw"); if (pf==NULL) { perror("fopen"); } // 写数据 int i; for ( i = 0; i <pc->sz; i++) { fwrite(pc->date + i, sizeof(struct address), 1, pf); } fclose(pf); pf = NULL; }
很简单的一个打开文件 写入数据
之后呢 我们在保存这个数据之后 我们希望 在每次打开通讯录之前 写入之前的数据
void loadcontact(struct contact* pc) { FILE* pf = fopen("test.dat", "rb"); if (pf == NULL) { perror("fopen"); } struct address tmp = { 0 }; while ((fread(&tmp,sizeof(struct address),1,pf))) { pc->date[pc->sz] = tmp; pc->sz++; if ((pc->sz) == (pc->max)) { pc->date = (struct address*)realloc(pc->date, ((pc->max) + 2) * sizeof(struct address)); printf("扩容成功\n"); pc->max += 2; } } }
我们这里可以发现 在初始化的时候可以将这些数据也初始化放进去
我们可以发现 原来的数据保存进来了
所有文件
1 测试文件
#define _CRT_SECURE_NO_WARNINGS 1 #include "address.h" int main() { struct contact c1; inicontact(&c1); int input = 1; do { menu(); scanf("%d", &input); switch (input) { case 1: system("cls"); addpeople(&c1); break; case 2: system("cls"); delpeople(&c1); break; case 3: system("cls"); srhpeople(&c1); break; case 4: system("cls"); revpeople(&c1); break; case 5: system("cls"); showpeople(&c1); break; case 6: system("cls"); inicontact(&c1); break; case 7: system("cls"); sortpeople(&c1); break; case 0: system("cls"); speople(&c1); killpeople(&c1); break; default: break; } } while (input); return 0; }
2 头文件
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include <windows.h> void menu(); void inicontact(struct contact* pc); void addpeople(struct contact* pc); void delpeople(struct contact* pc); void srhpeople(struct contact* pc); void revpeople(struct contact* pc); void showpeople(struct contact* pc); void sortpeople(struct contact* pc); void killpeople(struct contact* pc); int cmp(struct contact* pc); void speople(struct contact* pc); struct address { char name[10]; char sex[5]; int age; char tel[15]; char address[20]; }; struct contact { struct address* date; int sz; int max; }; void loadcontact(struct contact* pc);
3 主文件
#define _CRT_SECURE_NO_WARNINGS 1 #include "address.h" void menu() { printf("------------------------------------\n"); printf("--------请选择你要实现的功能--------\n"); printf("--------1.添加联系人信息------------\n"); printf("--------2.删除指定联系人信息--------\n"); printf("--------3.查找指定联系人信息--------\n"); printf("--------4.修改指定联系人信息--------\n"); printf("--------5.显示所有联系人信息--------\n"); printf("--------6.清空所有联系人------------\n"); printf("--------7.以名字排序所有联系人------\n"); } // 初版初始化 //void inicontact(struct contact* pc) //{ // pc->sz = 0; // memset(pc->date, 0, 100*sizeof(struct address)); //} // // 动态版本 //void inicontact(struct contact* pc) //{ // pc->sz = 0; // pc->max = 3; // pc->date = (struct address*)calloc(3, sizeof(struct address)); // // //} void loadcontact(struct contact* pc) { FILE* pf = fopen("test.dat", "rb"); if (pf == NULL) { perror("fopen"); } struct address tmp = { 0 }; while ((fread(&tmp,sizeof(struct address),1,pf))) { pc->date[pc->sz] = tmp; pc->sz++; if ((pc->sz) == (pc->max)) { pc->date = (struct address*)realloc(pc->date, ((pc->max) + 2) * sizeof(struct address)); printf("扩容成功\n"); pc->max += 2; } } fclose(pf); pf = NULL; } void inicontact(struct contact* pc) { pc->sz = 0; pc->max = 3; pc->date = (struct address*)calloc(3, sizeof(struct address)); loadcontact(pc); } // 初始版本的增加联系人 //void addpeople(struct contact* pc) //{ // if (pc->sz==100) // { // printf("通讯录已满 无法继续添加"); // return 0; // } // printf("请输入姓名\n"); // scanf("%s", &(pc->date[pc->sz].name)); // printf("请输入性别\n"); // scanf("%s", &(pc->date[pc->sz].sex)); // printf("请输入年龄\n"); // scanf("%d", &(pc->date[pc->sz].age)); // printf("请输入电话\n"); // scanf("%s", &(pc->date[pc->sz].tel)); // printf("请输入住址\n"); // scanf("%s", &(pc->date[pc->sz].address)); // printf("添加联系人完毕\n"); // pc->sz++; //} void addpeople(struct contact* pc) { if ((pc->sz) == (pc->max)) { pc->date = (struct address*)realloc(pc->date, ((pc->max) + 2) * sizeof(struct address)); printf("扩容成功\n"); pc->max += 2; } printf("请输入姓名\n"); scanf("%s", &(pc->date[pc->sz].name)); printf("请输入性别\n"); scanf("%s", &(pc->date[pc->sz].sex)); printf("请输入年龄\n"); scanf("%d", &(pc->date[pc->sz].age)); printf("请输入电话\n"); scanf("%s", &(pc->date[pc->sz].tel)); printf("请输入住址\n"); scanf("%s", &(pc->date[pc->sz].address)); printf("添加联系人完毕\n"); pc->sz++; } void delpeople(struct contact* pc) { char name1[10]; int input1; printf("请输入你要删除的联系人\n"); scanf("%s", &name1); int i; for (i = 0; i < (pc->sz); i++) { if (strcmp(pc->date[i].name, name1) == 0) { printf("查到到联系人%s是否确认删除(1or0)\n", name1); scanf("%d", &input1); if (input1 == 1) { for (int j = i; j < ((pc->sz) - 1); j++) { (pc->date[j]) = (pc->date[j + 1]); pc->sz--; } printf("删除成功\n"); } else { printf("成功取消\n"); } } } if (i >= (pc->sz)) { printf("联系人%s不存在\n", name1); } } void srhpeople(struct contact* pc) { char name1[10]; printf("请输入你要查找的联系人\n"); scanf("%s", &name1); int i; for (i = 0; i < (pc->sz); i++) { if (strcmp(pc->date[i].name, name1) == 0) { printf("%-10s\t %-15s\t %-3s\t %-15s\t %-20s\t\n", "name", "sex", "age", "tel", "address"); printf("%-10s\t %-15s\t %-3d\t %-15s\t %-20s\t\n", pc->date[i].name, pc->date[i].sex, pc->date[i].age, pc->date[i].tel, pc->date[i].address); } } if (i > pc->sz) { printf("不存在此联系人%s\n", name1); } } void revpeople(struct contact* pc) { char name1[10]; printf("请输入你要修改的联系人\n"); scanf("%s", &name1); int i; for (i = 0; i < (pc->sz); i++) { if (strcmp(pc->date[i].name, name1) == 0) { printf("请输入姓名\n"); scanf("%s", &(pc->date[i].name)); printf("请输入性别\n"); scanf("%s", &(pc->date[i].sex)); printf("请输入年龄\n"); scanf("%d", &(pc->date[i].age)); printf("请输入电话\n"); scanf("%s", &(pc->date[i].tel)); printf("请输入住址\n"); scanf("%s", &(pc->date[i].address)); printf("修改联系人完毕\n"); } } if (i > pc->sz) { printf("不存在此联系人%s\n", name1); } } void showpeople(struct contact* pc) { int i; printf("%-10s\t %-15s\t %-3s\t %-15s\t %-20s\t\n", "name", "sex", "age", "tel", "address"); for (i = 0; i < (pc->sz); i++) { printf("%-10s\t %-15s\t %-3d\t %-15s\t %-20s\t\n", pc->date[i].name, pc->date[i].sex, pc->date[i].age, pc->date[i].tel, pc->date[i].address); } } int cmp(void* e1, void* e2) { struct address* p1 = (struct address*)e1; struct address* p2 = (struct address*)e2; return strcmp(p1->name, p2->name); } void sortpeople(struct contact* pc) { qsort(pc, pc->sz, sizeof(struct address), cmp); } void killpeople(struct contact* pc) { free(pc->date); pc->date = NULL; pc->max = 0; pc->sz = 0; } void speople(struct contact* pc) { FILE* pf = fopen("test.dat", "wb"); if (pf==NULL) { perror("fopen"); } // 写数据 int i; for ( i = 0; i <pc->sz; i++) { fwrite(pc->date + i, sizeof(struct address), 1, pf); } printf("保存成功"); fclose(pf); pf = NULL; }
以上便是本文所有内容,如有错误请各位大佬不吝赐教,感谢留言