当前位置:网站首页>C language version - advanced address book (file version)

C language version - advanced address book (file version)

2022-08-11 05:47:00 Yunyi 943

前言

        在之前,I explained the project procedure of the address book management system,分别是:

Regular Address Book Items(Static development of memory version): CLanguage version address book operating system_

and Advanced Contacts items(Dynamic memory development version): C语言版通讯录——动态存储(进阶版)

        接下来,Let me talk about the address book of the file version again.,The advantage of the file version is that it can save the information of the debugged program.,Just like a small simple version of the database.

一.代码实现:

1.Save debug information and write to file

        First, you need to save the information filled in when the program is debugged,写入到文件中,需要用到一个函数SaveContact();Its place is in the branch statement that ends debugging:

 在Contact.c函数实现中:

void SaveContact(Contact* pc) {
    //create file and open,
	FILE* fw = fopen("Contact.txt", "wb");
	if (fw == NULL) {
		printf("%s\n", strerror(errno));
		return 1;
	}
	//写文件
	else {
		int i = 0;
		for (i = 0; i < pc->count; i++) {
			fwrite(pc->data + i, sizeof(PeoInf), 1, fw);
		}
	}

	//关闭文件
	fclose(fw);
	fw = NULL;
}

        代码讲解: To create a file pointer to receive firstfpoen函数创建出来的Contact.txt文件返回值,The file's open mode is"wb","wb"is a binary file writing method.然后通过ifstatement to determine whether the pointer opened the file successfully,若成功,则继续下一步:write information to a file.The steps to write to the file are done in a loop,将PeoInfMembers of the structure types of data to put into one,最后关闭文件.

        注:使用"wb"文件打开方式,打开时每次打开文件时会生成一个新的空文件覆盖原有的文件,则每次保存的时候都会重新录一遍信息,这样就可以通讯录的实时性,即删除联系人时再保存会把删除之后的联系人信息重新录进文件(原有的信息全部擦除)

增加两个成员:

           

 

 显示成员:

    

按0结束调试: 

 

 

文件内容: 

 

        如上图,The information provided through the program debugging throughfwritefunction to write to fileContact.txt中了,Because it is stored in binary form,So there will be some garbled(It doesn't matter).

2. Load information into the address book program

在Contact.c中:

//扩容
void check_key(Contact* pc) {
	assert(pc);
	if (pc->count == pc->capacity) {
		PeoInf* str = realloc(pc->data, (pc->capacity + 2) * sizeof(PeoInf));
		if (str == NULL) {
			printf("%s\n", strerror(errno));
			return 1;
		}
		else {
			pc->data = str;
			pc->capacity += 2;
			printf("增容成功\n");
		}
	}
}

//加载文件信息到通讯录中
void LoadContact(Contact* pc) {
	FILE* fr = fopen("Contact.txt", "rb");
	if (fr == NULL) {
		printf("%s\n", strerror(errno));
		return -1;
	}
	//读取文件信息
	PeoInf tmp = { 0 };
	while (fread(&tmp, sizeof(PeoInf), 1, fr) == 1) {
		check_key(pc);
		pc->data[pc->count] = tmp;
		pc->count++;
	}

		//关闭文件
	fclose(fr);
	fr = NULL;
}


int InitContact(Contact* pc) {
	assert(pc);
	pc->capacity = CA_begin;
	pc->count = 0;
	pc->data =(PeoInf*)calloc(CA_begin, sizeof(PeoInf));
	if (pc->data == NULL) {
		printf("%s\n", strerror(errno));
		return -1;
	}
	//加载文件信息到通讯录中
	LoadContact(pc);
	return 0;
}

        LoadContactThe function is used to realize the function of loading information into the address book program,It needs to be placed in the initialization address book functionInitContact()中,LoadContact()The implementation of the function is also created before opening through the file pointerContact.txt文件,使用freadThe function reads the information data of each person in the file,Need to call the expansion function.

调试结果:

 

二.完整代码实现:

test.c:

#define _CRT_SECURE_NO_WARNINGS 1
# include<stdio.h>
#include"Contact.h"

enum {
	EXIT,
	ADD,
	DELETE,
	MODIFY,
	SEARCH,
	SHOW,
	QSORT,
	TOTAL,
	CLEAN
};


void menu() {
	printf("**************Contacts management system interface***************\n");
	printf("********************0.Exit.********************\n");
	printf("********************1.Add. ********************\n");
	printf("********************2.Delete.******************\n");
	printf("********************3.Mod. ********************\n");
	printf("********************4.Search.******************\n");
	printf("********************5.Show.********************\n");
	printf("********************6.Sqort.*******************\n");
	printf("********************7.Total.*******************\n");
	printf("********************8.Clean.*******************\n");
}
int main() {
	int input = 0;
	Contact con;
	//初始化通讯录
	InitContact(&con);
	do {
		menu();
		printf("请输入你的选择:\n");
		scanf("%d", &input);
		switch (input) {
		case EXIT:
			SaveContact(&con);//文件保存
			FreeContact(&con);
			printf("即将退出程序\n");
			break;
		case ADD:
			AddContact(&con);
			break;
		case DELETE:
			DelContact(&con);
			break;
		case MODIFY:
			ModContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case QSORT:
			QsortContact(&con);
			break;
		case TOTAL:
			TotalContact(&con);
			break;
		case CLEAN:
			CleanContact(&con);
			break;
		default:
			printf("输入错误\n");
			break;
		}
	} while (input);
	return 0;
}

Contact.c:

#include"Contact.h"

//扩容
void check_key(Contact* pc) {
	assert(pc);
	if (pc->count == pc->capacity) {
		PeoInf* str = realloc(pc->data, (pc->capacity + 2) * sizeof(PeoInf));
		if (str == NULL) {
			printf("%s\n", strerror(errno));
			return 1;
		}
		else {
			pc->data = str;
			pc->capacity += 2;
			printf("增容成功\n");
		}
	}
}

//加载文件信息到通讯录中
void LoadContact(Contact* pc) {
	FILE* fr = fopen("Contact.txt", "rb");
	if (fr == NULL) {
		printf("%s\n", strerror(errno));
		return -1;
	}
	//读取文件信息
	PeoInf tmp = { 0 };
	while (fread(&tmp, sizeof(PeoInf), 1, fr) == 1) {
		check_key(pc);
		pc->data[pc->count] = tmp;
		pc->count++;
	}

		//关闭文件
	fclose(fr);
	fr = NULL;
}




int InitContact(Contact* pc) {
	assert(pc);
	pc->capacity = CA_begin;
	pc->count = 0;
	pc->data =(PeoInf*)calloc(CA_begin, sizeof(PeoInf));
	if (pc->data == NULL) {
		printf("%s\n", strerror(errno));
		return -1;
	}
	//加载文件信息到通讯录中
	LoadContact(pc);
	return 0;
}
	



//添加
void AddContact(Contact* pc){
	assert(pc);
	check_key(pc);
		printf("Preparing to add functional verification:\n");
		printf("开始添加:\n");
		printf("Please enter member name:\n");
		scanf("%s", pc->data[pc->count].name);
		printf("Please enter member age:\n");
		scanf("%d", &(pc->data[pc->count].age));
		printf("Please enter member gender:\n");
		scanf("%s", pc->data[pc->count].sex);
		printf("Please enter member phone number:\n");
		scanf("%s", pc->data[pc->count].tale);
		printf("Please enter member address:\n");
		scanf("%s", pc->data[pc->count].addr);
		pc->count++;
		printf("添加成功\n");
	}

//显示
void ShowContact(Contact* pc) {
	assert(pc);
	if (pc->count == 0) {
		printf("通讯录为空,Can't view members\n");
		return;
	}
	else {
		printf("%-20s\t %-4s\t %-5s\t %-12s\t %-20s\n", "姓名", "年龄", "性别", "电话", "地址");
		int i = 0;
		for (i = 0; i < pc->count; i++) {
			printf("%-20s\t %-4d\t %-5s\t %-12s\t %-20s\n", pc->data[i].name,
				pc->data[i].age,
				pc->data[i].sex,
				pc->data[i].tale,
				pc->data[i].addr
			);
		}
		
	}
}

//查找函数
int Find_name(Contact* pc, char name[]) {
	assert(pc);
	int i = 0;
	for (i = 0; i < pc->count; i++) {
		if (strcmp(pc->data[i].name, name) == 0) {
			return i;
		}
	}
	return -1;
}


//删除
void DelContact(Contact* pc) {
	assert(pc);
	char name[MAX_NAME] = { 0 };
	if (pc->count == 0) {
		printf("通讯录为空,无法进行删除\n");
		return;
	}
	else {
		printf("请输入要删除人的名字:\n");
		scanf("%s", name);
		int ret = Find_name(pc, name);
		if (ret == -1) {
			printf("该成员不存在\n");
			return;
		}
		else {
			printf("Preparing to remove functional verification:\n");
			int i = 0;
			for (i = ret; i < pc->count - 1; i++) {
				pc->data[i] = pc->data[i + 1];
			}
			pc->count--;
			printf("删除成功\n");
		}
	}
}

void ModContact(Contact* pc) {
	assert(pc);
	char name[MAX_NAME] = { 0 };
	if (pc->count == 0) {
		printf("通讯录为空,无法进行修改\n");
		return;
	}
	else {
		printf("Please enter the list of people you want to modify:\n");
		scanf("%s", name);
		int ret = Find_name(pc, name);
		if (ret == -1) {
			printf("该成员不存在\n");
			return;
		}
		else {
			printf("Modifying functional verification in progress:\n");
			printf("修改开始:\n");
			printf("Please enter member name:\n");
			scanf("%s", pc->data[ret].name);
			printf("Please enter member age:\n");
			scanf("%d", &(pc->data[ret].age));
			printf("Please enter member gender:\n");
			scanf("%s", pc->data[ret].sex);
			printf("Please enter member phone number:\n");
			scanf("%s", pc->data[ret].tale);
			printf("Please enter member address:\n");
			scanf("%s", pc->data[ret].addr);
			printf("\n修改成功\n");
		}
	}
}

//查找功能
void SearchContact(Contact* pc) {
	assert(pc);
	char name[MAX_NAME] = { 0 };
	if (pc->count == 0) {
		printf("通讯录为空,无法进行查找\n");
		return;
	}
	else {
		printf("Please enter a list of people you are looking for:\n");
		scanf("%s", name);
		int ret = Find_name(pc, name);
		if (ret == -1) {
			printf("该成员不存在\n");
			return;
		}
		else {
			printf("%-20s\t %-4s\t %-5s\t %-12s\t %-20s\n", "姓名", "年龄", "性别", "电话", "地址");
			printf("%-20s\t %-4d\t %-5s\t %-12s\t %-20s\n", pc->data[ret].name,
				pc->data[ret].age,
				pc->data[ret].sex,
				pc->data[ret].tale,
				pc->data[ret].addr
			);
			printf("\n查找成功\n");

		}
	}
}

int cmp_by_name(const void* e1, const void* e2) {
	return strcmp(((PeoInf*)e1)->name, ((PeoInf*)e2)->name);
}

int cmp_by_age(const void* e1, const void* e2) {
	return ((PeoInf*)e1)->age-((PeoInf*)e2)->age;
}


//排序功能
void QsortContact(Contact* pc) {
	assert(pc);
	int i = 0;
	printf("1.按姓名排序            2.按年龄排序       \n");
	printf("Please enter which sort method you want to use?\n");
	scanf("%d", &i);
	if (i == 1) {
		qsort(pc->data, pc->count, sizeof(PeoInf), cmp_by_name);
	}
	else if (i == 2) {
		qsort(pc->data, pc->count, sizeof(PeoInf), cmp_by_age);
	}
	printf("排序成功\n");
}

//统计
void TotalContact(Contact* pc) {
	assert(pc);
	if (pc->count == 0) {
		printf("通讯录为空,人员为0\n");
		return;
	}
	int count = pc->count;
	printf("At this time, there are a total of%d个成员\n", count);
}


	int Clean(Contact * pc) {
		assert(pc);
		pc->capacity = CA_begin;
		pc->count = 0;
		pc->data = (PeoInf*)calloc(CA_begin, sizeof(PeoInf));
		if (pc->data == NULL) {
			printf("%s\n", strerror(errno));
			return -1;
		}
		return 0;
	}

//一键清空
void CleanContact(Contact* pc) {
	assert(pc);
	if (pc->count == 0) {
		printf("Contacts are empty,This action can no longer be performed\n");
		return;
	}
	int i=0;
	printf("Are you sure you want to clear the address book??确认请按9,否则按9any key other than\n");
	scanf("%d",&i);
	if (i == 9) {
		Clean(pc);
		printf("清空成功\n");
	}
	else if (i == 8) {
		return;
	}
}

void SaveContact(Contact* pc) {
	FILE* fw = fopen("Contact.txt", "wb");
	if (fw == NULL) {
		printf("%s\n", strerror(errno));
		return 1;
	}
	//写文件
	else {
		int i = 0;
		for (i = 0; i < pc->count; i++) {
			fwrite(pc->data + i, sizeof(PeoInf), 1, fw);
		}
	}

	//关闭文件
	fclose(fw);
	fw = NULL;
}



void FreeContact(Contact* pc) {
	free(pc->data);
	pc->data = NULL;
}

Contact.h:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
# include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<errno.h>

#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TALE 12
#define MAX_ADDR 20

#define  CA_begin 3
#define Add_every 2


typedef struct PeoInf {
	char name[MAX_NAME];
	int age;
	char sex[MAX_SEX];
	char tale[MAX_TALE];
	char addr[MAX_ADDR];
}PeoInf;

typedef struct Contact {
	 PeoInf* data;
	int count;
	int capacity;
}Contact;


int InitContact(Contact* pc);
void AddContact(Contact* pc);
void ShowContact(Contact* pc);
void DelContact(Contact* pc);
void ModContact(Contact* pc);
void SearchContact(Contact* pc);
void QsortContact(Contact* pc);
void TotalContact(Contact* pc);
void CleanContact(Contact* pc);
void FreeContact(Contact* pc);


//文件保存
void saveContact(Contact* pc);

 

原网站

版权声明
本文为[Yunyi 943]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110512554309.html