C语言实现通讯录(使用文件保存信息)

这两天把动态内存还有文件函数这部分的课听了一下,也跟着把通讯录升级了一下
存储元素不再是固定的了,当存储的元素等于上限时内存就动态增长
每次退出信息又会销毁,可以用fwrite 和 fread让它保存到文件中,不过每次fwrite它就会覆盖之前的内容,所以每次初始化的时候都要先读取出来,等退出了再一次性读取写入文件中,数量多的话速度就会慢,想实现一下,今晚就下了个MySQL,听听网课琢磨琢磨
把这个改进版记录下来

int main()
{
   
	srand((unsigned int)time(NULL));
	game();
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>


#define MAX_NAME 16
#define MAX_SAT 6
#define MAX_TELE 12
#define MAX_ADDR 15
//#define MAX 100
#define DEFAULT_SZ 3

定义在头文件的函数声明
各个的作用都写下来了


void game();
void menu();//菜单
void chushihua(struct uct* p1);
void addformation(struct uct* p1);//增加信息
void showformation(struct uct* p1);//显示信息
void subformation(struct uct* p1);//删除信息
void searchformation(const struct uct* p1);//查找信息
void modifyformation(struct uct* p1);//改动信息
int cmp_stu_name(const void* p1, const void* p2);//比较结构体内的姓名
void IncreasetCapacity(struct uct*p1);//检测存储是否满了
void realise(struct uct* p1);//释放动态内存
void savecontact(struct uct* p1);//保存信息
void loadcontact(struct uct* p1);//从文件中读取信息


enum arr
{
   
	EXIT,
	ADD,
	SUB,
	SEARCH,
	MODIFY,
	SHOW,
	SORT,
	SAVE
};
struct information
{
   
	char name[MAX_NAME];
	int age;
	char set[MAX_SAT];
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
};
struct uct
{
   
	struct information *data;
	int size;
	int capacity;
};

我一般喜欢函数从上往下实现

void menu()
{
   
	printf("*******************************\n");
	printf("*****1 add           2 sub*****\n");
	printf("*****3 search        4 modify**\n");
	printf("*****5 show          6 sort****\n");
	printf("*****7 save          0 exit****\n");
	printf("*******************************\n");
}
void IncreasetCapacity(struct uct* p1);//动态增加信息

void loadcontact(struct uct* p1)
{
   
	struct information tmp = {
    0 };
	FILE* pfRead = fopen("test.dat"
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用文件保存通讯录信息,这样可以实现信息的持久化存储。具体实现方法如下: 1. 定义联系人结构体 ```c typedef struct { char name[20]; char phone[12]; char email[30]; } Contact; ``` 2. 定义文件名和文件打开方式 ```c #define FILENAME "contacts.txt" #define MODE "r+b" ``` 3. 实现添加联系人函数 ```c void add_contact() { Contact c; FILE* fp = fopen(FILENAME, MODE); if (fp == NULL) { printf("Failed to open file\n"); return; } printf("Enter name: "); scanf("%s", c.name); printf("Enter phone number: "); scanf("%s", c.phone); printf("Enter email: "); scanf("%s", c.email); fwrite(&c, sizeof(Contact), 1, fp); printf("Contact added successfully.\n"); fclose(fp); } ``` 4. 实现查找联系人函数 ```c void find_contact() { char name[20]; Contact c; FILE* fp = fopen(FILENAME, MODE); if (fp == NULL) { printf("Failed to open file\n"); return; } printf("Enter name to search: "); scanf("%s", name); while (fread(&c, sizeof(Contact), 1, fp) != 0) { if (strcmp(c.name, name) == 0) { printf("Name: %s\n", c.name); printf("Phone: %s\n", c.phone); printf("Email: %s\n", c.email); fclose(fp); return; } } printf("Contact not found.\n"); fclose(fp); } ``` 5. 实现删除联系人函数 ```c void delete_contact() { char name[20]; Contact c; FILE* fp = fopen(FILENAME, MODE); if (fp == NULL) { printf("Failed to open file\n"); return; } printf("Enter name to delete: "); scanf("%s", name); FILE* tmp = tmpfile(); while (fread(&c, sizeof(Contact), 1, fp) != 0) { if (strcmp(c.name, name) != 0) { fwrite(&c, sizeof(Contact), 1, tmp); } } fclose(fp); fp = fopen(FILENAME, "wb"); rewind(tmp); while (fread(&c, sizeof(Contact), 1, tmp) != 0) { fwrite(&c, sizeof(Contact), 1, fp); } fclose(fp); printf("Contact deleted successfully.\n"); } ``` 6. 实现修改联系人函数 ```c void update_contact() { char name[20]; Contact c; FILE* fp = fopen(FILENAME, MODE); if (fp == NULL) { printf("Failed to open file\n"); return; } printf("Enter name to update: "); scanf("%s", name); FILE* tmp = tmpfile(); while (fread(&c, sizeof(Contact), 1, fp) != 0) { if (strcmp(c.name, name) == 0) { printf("Enter new name: "); scanf("%s", c.name); printf("Enter new phone number: "); scanf("%s", c.phone); printf("Enter new email: "); scanf("%s", c.email); } fwrite(&c, sizeof(Contact), 1, tmp); } fclose(fp); fp = fopen(FILENAME, "wb"); rewind(tmp); while (fread(&c, sizeof(Contact), 1, tmp) != 0) { fwrite(&c, sizeof(Contact), 1, fp); } fclose(fp); printf("Contact updated successfully.\n"); } ``` 7. 实现显示所有联系人函数 ```c void show_all_contacts() { Contact c; FILE* fp = fopen(FILENAME, MODE); if (fp == NULL) { printf("Failed to open file\n"); return; } while (fread(&c, sizeof(Contact), 1, fp) != 0) { printf("Name: %s\n", c.name); printf("Phone: %s\n", c.phone); printf("Email: %s\n", c.email); printf("\n"); } fclose(fp); } ``` 注意事项: 1. 文件打开方式为 "r+b",表示以读写方式打开二进制文件,如果文件不存在则创建。 2. 查找、删除和修改联系人时需要使用临时文件进行操作,最后再将临时文件内容复制回原文件。 3. 代码中没有对输入的数据进行校验和验证,需要自行添加。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值