C language implementation address book (file version)

1. After learning the C language file, you can add the following functions of the address book

After learning how to read and write files in C language, we can add the following functions to the address book:


1. When exiting the address book, write the added contact information into the file.

2. When the address book is initialized, load (read) the information in the file to the address book——(directly use the print function to display on the screen)

2. Analyze and implement corresponding functions

analyze:

  1. When we exit the address book, we need to realize the save function of the address book (realize saving the contact information of the address book to the Contact.dat file) - SaveContact

  1. When we run the address book again and add, delete, check and modify the address book, we should output the information in the file to the running box and display it——LoadContact

  1. When we implemented the LoadContact function, we found that the loading function LoadContact must be called in the initialization address book function.

  1. When using the LoadContact function, we found a problem - if the number of contacts in the file is greater than 3 (capacity), then we read the file into the program and found that the information of all the people in the file cannot be displayed. At this time, we need to expand the capacity first in the LoadContact function, and then read.
  1. We found that many places need to use expansion, so we simply implement an expansion function.

accomplish:

Call LoadContact in the initialization function

void InitContact( Contact* ps)
{
    
    
	assert(ps);
	ps->data = (PeoInfo*)malloc(default_sz * sizeof(PeoInfo));
	if (ps->data == NULL)
	{
    
    
		perror("InitContact");
		return;
	}
	ps->size = 0;
	ps->capacity = default_sz;

	//把文件中已经存放的通讯录中的信息加载到通讯录中
	LoadContact(ps);
}

SaveContact function (save contact information to a file)

SaveContact(Contact* ps)
{
    
    
	assert(ps);
	//打开文件,以写的形式打开--(没有contact.dat的文件会自动创建一个)
	FILE* pf = fopen("Contact.dat", "w");
	if (pf == NULL)
	{
    
    
		perror("SaveContact");
		return;
	}
	//写文件
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
    
    
		fwrite(ps->data + i, sizeof(PeoInfo), 1, pf);
	}
	//关闭文件
	fclose(pf);
	pf = NULL;
}

LoadContact function (read file information to address book)

void LoadContact(Contact* ps)
	{
    
    
		FILE* pf = fopen("Contact.dat", "r");
		if (pf== NULL)
		{
    
    
			printf("LoadContact:");
			return;
		}

		//读取文件,存放到通讯录中
		PeoInfo tmp = {
    
     0 };
		while (fread(&tmp, sizeof(PeoInfo), 1, pf))
		{
    
    
			CheckCapacity(ps);//要设置扩容(调用扩容函数)
			ps->data[ps->size] = tmp;
			ps->size++;
		}
		fclose(pf);
		pf = NULL;

	}

Check the expansion function

void CheckCapacity(struct Contact* ps)
{
    
    
	assert(ps);
	if (ps->size == ps->capacity)
	{
    
    
		//增容
		struct PeoInfo* ptr = realloc(ps->data, (ps->capacity + 2) * sizeof(PeoInfo));
		if (ptr != NULL)
		{
    
    
			ps->data = ptr;
			ps->capacity += 2;
			printf("增容成功\n");
		}
		else
		{
    
    
			printf("增容失败\n");
		}
	}
}

3. Program running results

Select 5 directly to display the previous contact information
insert image description here

Guess you like

Origin blog.csdn.net/qq_58286439/article/details/130001481