Address book (C language file version) (super detailed process)

Please add image description

?
Different beliefs
Decide different fates

Address Book

?Features

Required header file name

#include<stdio.h>//The first one
#include<string.h>//The second one
#include<stdlib.h>//The third one

The first header file name is a regular header file name, such as int main(). Both the main function and the printf function are required. This header file name is required whenever writing C language.

The second header file name is used when you need to use the strcmp function when executing the address book file below.

The third header file name will also be used when executing the address book file below, such as: qsort() function and free() function will be used

Of course, the purpose of these functions will be introduced when explaining the address book.

Other functions and related structures used

typedef struct Proper//Stored person information
{<!-- -->
char name[20];//name
int age;//Age
char number[20];//phone number
}Proper;

typedef struct Contact space for storing person information
{<!-- -->
Proper* date;//Pointer to store person information
int se;//The number of contact information stored in the space
int eff;//The total size of the space
}Contact;

void Inialize(Contact* con)//Initialization of array to store contact information
{<!-- -->
con->date = (Proper*)malloc(3 * sizeof(Proper));
if (con->date == NULL)
{<!-- -->
perror("Inialize");
return;
}
con->se = 0;
con->eff = 3;
}

void menu()//Game menu
{<!-- -->
printf("******************************\
");
printf("***** 1. Add contact ***********\
");
printf("***** 2. Delete contact *************\
");
printf("***** 3. Display contact *************\
");
printf("***** 4. Find contact *************\
");
printf("***** 5. Modify contact *************\
");
printf("***** 6. Contact sorting *************\
");
printf("***** 7. Clear contacts *************\
");
printf("***** 8. Exit address book ***********\
");
printf("******************************\
");
}
void test()//Address book function
{<!-- -->
int input = 0;
Contact con;
Inialize( & amp;con);//Initialize address book
do
{<!-- -->
menu();//Game menu
printf("Please make a selection:>");
scanf("%d", & amp;input);
switch(input)
{<!-- -->
case 1:
AddContact( & amp;con);//Add contact
break;
case 2:
Delete(& amp;con);//Delete contact
break;
case 3:
Display( & amp;con);//Display contacts
break;
case 4:
SeekContact( & amp;con);//Find the contact
break;
case 5:
Modify( & amp;con);//Modify contact
break;
case 6:
SortContact( & amp;con);//Contact sorting
break;
case 7:
empty(& amp;con);
break;
case 8:
SaveContact( & amp;con);
realse( & amp;con);
printf("Exit address book\
");
break;
default:
printf("Incorrect input, please re-enter\
");
break;
}
if (input == 8)
input = 0;
} while (input);
}
int main()//main function
{<!-- -->
test();
return 0;
}

Add contact

int Addeff(Contact* con)//Function to check whether the storage space is full
{<!-- -->
if (con->se == con->eff)//It will be full when the two are equal, except for the first time when both are 0
{<!-- -->
Proper* per = (Proper*)realloc(con->date, (con->eff + 2) * sizeof(Proper));
if (per == NULL)//Determine whether the creation of per space is successful
{<!-- -->
perror("Addeff");
return 0;
}
con->date = per;
con->eff = con->eff + 2;
printf("Capacity expansion successful\
");
}
return 1;
}
void AddContact(Contact* con)//Add contact function
{<!-- -->
if (Addeff(con) == 0)//If the Addeff function returns 0, it means that the space creation failed and the contact cannot be added.
{<!-- -->
return;
}

printf("Enter name:>");
scanf("%s", con->date[con->se].name);

printf("Enter age:>");
scanf("%d", & amp;con->date[con->se].age);//What will add & amp; (address character) here? This is because the input is a number, and There is no need to add & amp; in the above input because it is an array. The first element of the array is the size of the array
\t
printf("Enter phone number:>");
\t
scanf("%s", con->date[con->se].number);
\t
con->se + + ;
printf("Added successfully\
");
}

The Addeff function is used to check whether the space we use to store contacts is full. If it is full, this function will be used to increase the space. The Contact() function is used to add contacts

Delete contact

void Delete(Contact* con)//Delete contact function
{<!-- -->
int i = 0;
char m[20] = {<!-- --> 0 };//Create a new array to enter the space of the contact you need to find
printf("Enter the name of the contact you want to delete: >");
scanf("%s", m);
for (i = 0; i < con->se; i + + )//Find the information of the contact you need to delete in the data
{<!-- -->
int j = 0;
if (strcmp(m, con->date[i].name) == 0)
{<!-- -->
for (j = i; j < con->se - 1; j + + )//When you find the message of the contact you want to delete, use the following data to overwrite this data, so as to achieve the purpose of deletion. effect
{<!-- -->

con->date[j] = con->date[j + 1];
}
con->se--;//When you delete a contact, the data behind the contact is moved forward, so there is one more space at the end. At this time we need to delete this space
printf("Deletion successful\
");
}
}
}

This process is easy to understand, because the contact information stored is an array, so we can only delete it by overwriting.

Show contacts

void Display(Contact* con)//Function to print all contact information in the address book
{<!-- -->
int i = 0;
printf("%-10s %-10s %-10s\
", "Name", "Age", "Telephone");
for (i = 0; i < con->se; i + + )
{<!-- -->
printf("%-10s %-10d %-10s\
", con->date[i].name, con->date[i].age, con->date[i].number);
}
}

Find a contact

void SeekContact(Contact* con)
{<!-- -->
int i = 0;
char m[20] = {<!-- --> 0};//It has the same meaning as the variable you need to create to delete a contact. Here it stores the information of the contact you need to find.
printf("Enter the name of the contact you are looking for: >");
scanf("%s", m);
for (i = 0; i < con->se; i + + )//Traverse it once
{<!-- -->
if (strcmp(m, con->date[i].name) == 0)//Using the strcmp function
{<!-- -->
printf("Found: Name: %s, Age: %d, Number: %s\
", con->date[i].name, con->date[i].age, con->date[i ].number);
}
}
}

strcmp is a function used to compare the size of strings. Its usage is introduced in the blog I wrote before. You can check it out: Library function simulation of characters and strings (this is a link. If you need it, you can click on it directly. )

Modify contact

void Modify(Contact* con)//Function to modify contact information
{<!-- -->
char m[20] = {<!-- --> 0 };//The usage here is similar to the above, used to store the information you need to find contacts
printf("Enter the name of the contact you need to modify: >");
scanf("%s", m);
int i = 0;
for (i = 0; i < con->se; i + + )//Traverse it once
{<!-- -->
if (strcmp(m, con->date[i].name) == 0)//Using the strcmp function
{<!-- -->
printf("Re-enter name:>");
scanf("%s", con->date[i].name);

printf("Re-enter age:>");
scanf("%d", & amp;con->date[i].age);//What will add & amp; (address character) here? This is because the input is a number, and the input above does not You need to add & amp; because it is an array. The first element of the array is the size of the array

printf("Re-enter number:>");
scanf("%s", con->date[i].number);

printf("Modification successful\
");
}
}
}

Contact sorting

int compare(const void* p1, const void* p2)//Comparison function
{<!-- -->
return strcmp(((Proper*)p1)->name, ((Proper*)p2)->name);
}

void SortContact(Contact* con)//Function used to sort contacts
{<!-- -->
qsort(con->date, con->se, sizeof(Proper), compare);//This is a function specifically for sorting
printf("Successfully sorted\
");
}

qsort() function: You can refer to related blogs or Baidu. I won’t explain too much here (sorry)
compare() function: This function is used in conjunction with the qsort function. Refer to relevant information. (Feel sorry)

Clear contacts

void empty(Contact* con)//Function to clear contacts
{<!-- -->
con->se = 0;//Because con->se represents the size of the array space class, when the space is 0, there will be no data in this group
printf("cleared successfully\
");
}

Exit the address book

The program exits when the input input is 0.

The address book is blank

void realse(Contact* con)//Function to empty the address book space
{<!-- -->
free(con->date);//Empty function, as long as the space is dynamically opened, you need to use this function to empty it, otherwise there will be a memory leak.
con->date = NULL;//are used together with free
con->se = 0;
con->eff = 0;
}

Regarding the usage of free() function. You can read my previous blog: Dynamic Memory Management (C Language) (This is a link. If you are not sure, you can click on it to view it. There is a detailed introduction to Intelligent Memory)

Ordinary address book implementation

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Proper
{<!-- -->
char name[20];
int age;
char number[20];
}Proper;

typedef struct Contact
{<!-- -->
Proper* date;
int se;
int eff;
}Contact;

void menu()
{<!-- -->
printf("******************************\
");
printf("***** 1. Add contact ***********\
");
printf("***** 2. Delete contact *************\
");
printf("***** 3. Display contact *************\
");
printf("***** 4. Find contact *************\
");
printf("***** 5. Modify contact *************\
");
printf("***** 6. Contact sorting *************\
");
printf("***** 7. Clear contacts *************\
");
printf("***** 8. Exit address book ***********\
");
printf("******************************\
");
}

void Inialize(Contact* con)
{<!-- -->
con->date = (Proper*)malloc(3 * sizeof(Proper));
if (con->date == NULL)
{<!-- -->
perror("Inialize");
return;
}
con->se = 0;
con->eff = 3;
}

int Addeff(Contact* con)
{<!-- -->
if (con->se == con->eff)
{<!-- -->
Proper* per = (Proper*)realloc(con->date, (con->eff + 2) * sizeof(Proper));
if (per == NULL)
{<!-- -->
perror("Addeff");
return 0;
}
con->date = per;
con->eff = con->eff + 2;
printf("Capacity expansion successful\
");
}
return 1;
}

void AddContact(Contact* con)
{<!-- -->
if (Addeff(con) == 0)
{<!-- -->
return;
}

printf("Enter name:>");
scanf("%s", con->date[con->se].name);

printf("Enter age:>");
scanf("%d", & amp;con->date[con->se].age);

printf("Enter phone number:>");
scanf("%s", con->date[con->se].number);

con->se + + ;

printf("Added successfully\
");

}

void Display(Contact* con)
{<!-- -->
int i = 0;
printf("%-10s %-10s %-10s\
", "name", "age", "phone");
for (i = 0; i < con->se; i + + )
{<!-- -->
printf("%-10s %-10d %-10s\
", con->date[i].name, con->date[i].age, con->date[i].number);
}
}

void Delete(Contact* con)
{<!-- -->
int i = 0;
char m[20] = {<!-- --> 0 };
printf("Enter the name of the contact you want to delete: >");
scanf("%s", m);
for (i = 0; i < con->se; i + + )
{<!-- -->
int j = 0;
if (strcmp(m, con->date[i].name) == 0)
{<!-- -->
for (j = i; j < con->se - 1; j + + )
{<!-- -->

con->date[j] = con->date[j + 1];
}
con->se--;
printf("Deletion successful\
");
}
}
}

void SeekContact(Contact* con)
{<!-- -->
int i = 0;
char m[20] = {<!-- --> 0 };
printf("Enter the name of the contact you are looking for: >");
scanf("%s", m);
for (i = 0; i < con->se; i + + )
{<!-- -->
if (strcmp(m, con->date[i].name) == 0)
{<!-- -->
printf("Found: Name: %s, Age: %d, Number: %s\
", con->date[i].name, con->date[i].age, con->date[i ].number);
}
}
}

void Modify(Contact* con)
{<!-- -->
char m[20] = {<!-- --> 0 };
printf("Enter the name of the contact you need to modify: >");
scanf("%s", m);
int i = 0;
for (i = 0; i < con->se; i + + )
{<!-- -->
if (strcmp(m, con->date[i].name) == 0)
{<!-- -->
printf("Re-enter name:>");
scanf("%s", con->date[i].name);

printf("Re-enter age:>");
scanf("%d", & amp;con->date[i].age);

printf("Re-enter number:>");
scanf("%s", con->date[i].number);

printf("Modification successful\
");
}
}
}

int compare(const void* p1, const void* p2)
{<!-- -->
return strcmp(((Proper*)p1)->name, ((Proper*)p2)->name);
}

void SortContact(Contact* con)
{<!-- -->
qsort(con->date, con->se, sizeof(Proper), compare);
printf("Successfully sorted\
");
}

void empty(Contact* con)
{<!-- -->
con->se = 0;
printf("cleared successfully\
");
}

void realse(Contact* con)
{<!-- -->
free(con->date);
con->date = NULL;
con->se = 0;
con->eff = 0;
}

void test()
{<!-- -->
int input = 0;
Contact con;
Inialize( & amp;con);//Initialize address book
do
{<!-- -->
menu();//Game menu
printf("Please make a selection:>");
scanf("%d", & amp;input);
switch(input)
{<!-- -->
case 1:
AddContact( & amp;con);//Add contact
break;
case 2:
Delete(& amp;con);//Delete contact
break;
case 3:
Display( & amp;con);//Display contacts
break;
case 4:
SeekContact( & amp;con);//Find the contact
break;
case 5:
Modify( & amp;con);//Modify contact
break;
case 6:
SortContact( & amp;con);//Contact sorting
break;
case 7:
empty(& amp;con);
break;
case 8:
realse( & amp;con);
printf("Exit address book\
");
break;
default:
printf("Incorrect input, please re-enter\
");
break;
}
if (input == 8)
input = 0;
} while (input);
}

int main()
{<!-- -->
test();
return 0;
}

The above is the implementation code of ordinary address book. Just copy and paste if necessary.

?Dynamic file operation implementation process

Contact information is saved in the file

void SaveContact(Contact* con)
{<!-- -->
FILE* fp = fopen("C:\Users\Yang Peiqi\Desktop\contact.txt", "wb");//This is the creation of the file
if (fp == NULL)//This is the file judgment. If the created file is empty, the function will end directly and the next step will not be performed.
{<!-- -->
perror("fopen");
return;
}
for (int i = 0; i < con->se; i + + )
{<!-- -->
fwrite( & amp;(con->date[i]), sizeof(Proper), 1, fp);//This is to store the array data into the file fp we just created
}
fclose(fp);//The file is empty, the usage is the same as free, except that fclose is used to empty the file, and free is used for dynamic space
fp = NULL;//and fclose are used together

}

If you don’t know much about file operations, such as the usage of FILE, fwrite, fread, etc., you can watch my blog about the detailed file operation process (C language) (this is a link for everyone who knows about files) You can click on the shortcomings to see).

Read contact information from a file

void LoadContact(Contact* con)//Read the contact information in the file
{<!-- -->
FILE* fp = fopen("C:\Users\Peter Yang\Desktop\contact.txt", "rb");
if (fp == NULL)
{<!-- -->
perror("LoadContact");
return;
}
Proper tem = {<!-- --> 0 };
while (fread( & amp; tem, sizeof(Proper), 1, fp))//fread() function is a function that reads files
{<!-- -->
if (0 == Addeff(con))
return;
con->date[con->se] = tem;
con->se + + ;
}
//Why put the function in the while loop? This is because the return value of fread is an integer. It will return 1 when reading one piece of data, 2 when reading two pieces, and so on. When the read data is empty, it will return 0.
fclose(fp);//The blanking function is the same as free, except that fclose is to blank the file and free is to blank the dynamic space.
fp = NULL;//Use with fclose
}
void Inialize(Contact* con)//Initialization of array to store contact information
{<!-- -->
con->date = (Proper*)malloc(3 * sizeof(Proper));
if (con->date == NULL)
{<!-- -->
perror("Inialize");
return;
}
con->se = 0;
con->eff = 3;
LoadContact(con); //There is only one more function than the initialization in ordinary address book implementation. When we need to read the contact information stored in the file before the program is executed, it can only be implemented during the initialization process.
}

If you don’t know much about file operations, such as the usage of FILE, fwrite, fread, etc., you can watch my blog’s detailed file operation process (C language) (this is a link for everyone who knows about files) You can click on the shortcomings to see).

Address book (file form) implementation process

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct Proper
{<!-- -->
char name[20];
int age;
char number[20];
}Proper;

typedef struct Contact
{<!-- -->
Proper* date;
int se;
int eff;
}Contact;

void menu()
{<!-- -->
printf("******************************\
");
printf("***** 1. Add contact *************\
");
printf("***** 2. Delete contact *************\
");
printf("***** 3. Display contact *************\
");
printf("***** 4. Find contact *************\
");
printf("***** 5. Modify contact *************\
");
printf("***** 6. Contact sorting *************\
");
printf("***** 7. Clear contacts *************\
");
printf("***** 8. Exit address book ***********\
");
printf("******************************\
");
}

int Addeff(Contact* con);//Because the Addeff function is below when it is created, and it will not be found when the function before it needs to be called, so you need to write a statement in advance. In C language, the order of function execution is from top to bottom.

void LoadContact(Contact* con)
{<!-- -->
FILE* fp = fopen("C:\Users\Peter Yang\Desktop\contact.txt", "rb");
if (fp == NULL)
{<!-- -->
perror("LoadContact");
return;
}
Proper tem = {<!-- --> 0 };
while (fread( & amp; tem, sizeof(Proper), 1, fp))
{<!-- -->
if (0 == Addeff(con))
return;
con->date[con->se] = tem;
con->se + + ;
}
fclose(fp);
fp = NULL;
}

void Inialize(Contact* con)
{<!-- -->
con->date = (Proper*)malloc(3 * sizeof(Proper));
if (con->date == NULL)
{<!-- -->
perror("Inialize");
return;
}
con->se = 0;
con->eff = 3;
LoadContact(con);
}

int Addeff(Contact* con)
{<!-- -->
if (con->se == con->eff)
{<!-- -->
Proper* per = (Proper*)realloc(con->date, (con->eff + 2) * sizeof(Proper));
if (per == NULL)
{<!-- -->
perror("Addeff");
return 0;
}
con->date = per;
con->eff = con->eff + 2;
printf("Capacity expansion successful\
");
}
return 1;
}

void AddContact(Contact* con)
{<!-- -->
if (Addeff(con) == 0)
{<!-- -->
return;
}

printf("Enter name:>");
scanf("%s", con->date[con->se].name);

printf("Enter age:>");
scanf("%d", & amp;con->date[con->se].age);

printf("Enter phone number:>");
scanf("%s", con->date[con->se].number);

con->se + + ;

printf("Added successfully\
");

}

void Display(Contact* con)
{<!-- -->
int i = 0;
printf("%-10s %-10s %-10s\
", "name", "age", "phone");
for (i = 0; i < con->se; i + + )
{<!-- -->
printf("%-10s %-10d %-10s\
", con->date[i].name, con->date[i].age, con->date[i].number);
}
}

void Delete(Contact* con)
{<!-- -->
int i = 0;
char m[20] = {<!-- --> 0 };
printf("Enter the name of the contact you want to delete: >");
scanf("%s", m);
for (i = 0; i < con->se; i + + )
{<!-- -->
int j = 0;
if (strcmp(m, con->date[i].name) == 0)
{<!-- -->
for (j = i; j < con->se - 1; j + + )
{<!-- -->

con->date[j] = con->date[j + 1];
}
con->se--;
printf("Deletion successful\
");
}
}
}

void SeekContact(Contact* con)
{<!-- -->
int i = 0;
char m[20] = {<!-- --> 0 };
printf("Enter the name of the contact you are looking for: >");
scanf("%s", m);
for (i = 0; i < con->se; i + + )
{<!-- -->
if (strcmp(m, con->date[i].name) == 0)
{<!-- -->
printf("Found: Name: %s, Age: %d, Number: %s\
", con->date[i].name, con->date[i].age, con->date[i ].number);
}
}
}

void Modify(Contact* con)
{<!-- -->
char m[20] = {<!-- --> 0 };
printf("Enter the name of the contact you need to modify: >");
scanf("%s", m);
int i = 0;
for (i = 0; i < con->se; i + + )
{<!-- -->
if (strcmp(m, con->date[i].name) == 0)
{<!-- -->
printf("Re-enter name:>");
scanf("%s", con->date[i].name);

printf("Re-enter age:>");
scanf("%d", & amp;con->date[i].age);

printf("Re-enter number:>");
scanf("%s", con->date[i].number);

printf("Modification successful\
");
}
}
}

int compare(const void* p1, const void* p2)
{<!-- -->
return strcmp(((Proper*)p1)->name, ((Proper*)p2)->name);
}

void SortContact(Contact* con)
{<!-- -->
qsort(con->date, con->se, sizeof(Proper), compare);
printf("Successfully sorted\
");
}

void empty(Contact* con)
{<!-- -->
con->se = 0;
printf("cleared successfully\
");
}

void realse(Contact* con)
{<!-- -->
free(con->date);
con->date = NULL;
con->se = 0;
con->eff = 0;
}

void SaveContact(Contact* con)
{<!-- -->
assert(con);
FILE* fp = fopen("C:\Users\Peter Yang\Desktop\contact.txt", "wb");
if (fp == NULL)
{<!-- -->
perror("fopen");
return;
}
for (int i = 0; i < con->se; i + + )
{<!-- -->
fwrite( & amp;(con->date[i]), sizeof(Proper), 1, fp);
}
fclose(fp);
fp = NULL;

}

void test()
{<!-- -->
int input = 0;
Contact con;
Inialize( & amp;con);//Initialize address book
do
{<!-- -->
menu();//Game menu
printf("Please make a selection:>");
scanf("%d", & amp;input);
switch(input)
{<!-- -->
case 1:
AddContact( & amp;con);//Add contact
break;
case 2:
Delete(& amp;con);//Delete contact
break;
case 3:
Display( & amp;con);//Display contacts
break;
case 4:
SeekContact( & amp;con);//Find the contact
break;
case 5:
Modify( & amp;con);//Modify contact
break;
case 6:
SortContact( & amp;con);//Contact sorting
break;
case 7:
empty(& amp;con);
break;
case 8:
SaveContact( & amp;con);
realse( & amp;con);
printf("Exit address book\
");
break;
default:
printf("Incorrect input, please re-enter\
");
break;
}
if (input == 8)
input = 0;
} while (input);
}

int main()
{<!-- -->
test();
return 0;
}

?
Let’s work hard together! !
?