Using C language to realize the file version of address book

1. Why file operation

In the past, we used C language to construct the address book, but whether it is a one-time open space address book or a dynamic version of the address book, we can't save the information after exiting the address book. However, through file operation, we can save the information in the file when we exit the address book, and read the information into the program when we open it again, so as to achieve the function of saving the information every time we open it.

2. How to save address book

In C language, there are file operation functions to help us save the information in the program to a file. In order to save the address book, we will use fopen, fclose, fwrite and fread functions. Through these four functions, we can write the information into the file when closing the program, and read the information in the file into the program when opening the program, so as to save the address book information.

Next, we will introduce these four functions.

FILE *fopen(const char *filename, const char *mode)

fopen function allows us to open a file. The first parameter is the string containing the file name. It can be the name of the file, such as "data.txt". When only the file name is written, the program will only find the file in the current directory. You can also include the path of the file, such as "C:\Users\Administrator\Desktop\data.txt", so that you can operate the files in other directories.

The second parameter is the string containing the file access mode. For example, "r" means to open the file by reading. If the file does not exist, it will fail to open. While "w" means to open a file by writing. If there is no file with the same name, an empty file will be created. If there is a file with the same name, it will be emptied.

Its return value will return a pointer of FILE * type, which points to the open FILE. If the opening fails, it will return NULL.

int fclose(FILE *stream)

The fclose function can help us close an open file. Its parameter is the file to be closed. Its return value will return 0 when the file is closed successfully, and EOF if it fails.

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

The fwrite function can write the data of the space pointed to to to the file. The parameter ptr points to the space of the data to be written. The parameter size refers to the size of each data to be written. The parameter nmemb refers to the total number of data to be written, and the size of each data is size. The parameter stream is the pointer to the file to be written. The return value of the fwrite function is the total number of data to be written.

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

The freead function reads the data in the file to the memory space, where the ptr pointer points to the memory space, the size parameter means the size of the space to be read for each data, and nmemb is the total number of data read. The stream pointer points to the file to be read. The return value of the freead function is the total number of data read.

3. Modify address book

After selecting exit, we create a SaveCon function to save the address book, and then destroy the address book.

case EXIT:
			SaveCon(&con);//Save address book to file
			DestroyCon(&con);
			printf("Exit address book.");
			break;

Internal implementation of SaveCon function

void SaveCon(struct contact* con)
{
	assert(con);//Assert
	FILE* pf = fopen("C:\\Users\\Administrator\\Desktop\\data.txt", "wb");//Open file
	if (pf == NULL)
	{
		perror("SaveContact");
		return;
	}
	fwrite(con->data, sizeof(struct peoinfo), con->size, pf);//Write information
	fclose(pf);//Close file
	pf = NULL;//Set pointer to null pointer
}

In this way, we can automatically save the information to a file when we close the address book.

We build a LoadCon function to read the information in the file when opening the address book.

int main()
{
	struct contact con;
	InitCon(&con);
	LoadCon(&con);//Read the information in the file into the address book
    //Follow
    //ring
    //Department
    //branch
    return 0;
}

Internal implementation of LoadCon function

void LoadCon(struct contact* con)
{
	assert(con);//Assert
	FILE* pf = fopen("C:\\Users\\Administrator\\Desktop\\data.txt", "rb");//Open file
	if (pf == NULL)
	{
		perror("LoadContact");
		return;
	}
	struct peoinfo tmp = { 0 };//Create temporary space to save information
	while (fread(&tmp, sizeof(struct peoinfo), 1, pf))//Jump out of the loop when there is no information to read
	{
		if (con->size == con->capacity)//Judge whether the capacity is sufficient. If not, increase the capacity
		{
			struct peoinfo* p = realloc(con->data, 
            (con->capacity + COM_CAP) * sizeof(struct peoinfo));
			if (p == NULL)
			{
				printf("Capacity increase failed\n");
				return;
			}
			con->data = p;
			con->capacity += COM_CAP;
		}
		con->data[con->size] = tmp;//Put information from the temporary space into the address book
		con->size++;
	}
	fclose(pf);
	pf = NULL;
}

Different from writing, when reading information, our size is 0, so we cannot directly read size information into the address book. Therefore, we first use a tmp structure to receive a person's information, and then judge whether the return value of the fread function is 0. If it is 0, it means that the information cannot be read in this reading. If it is not 0, write the information into the address book, and then read the next one People's information. It should also be noted here that the capacity of the address book after initialization may not fit everyone's information, so we should judge the capacity of the address book after reading everyone's information. If the capacity is not enough, we need to expand the capacity.

In this way, an address book that can save information to a file is transformed.

Tags: C

Posted on Mon, 04 Oct 2021 23:05:11 -0400 by matstuff