C contacts example, saving data into file

created at 02-08-2022 views: 6

preface

This time is the local implementation of the address book. You can save the input information in the program to the local file "test.dat" and open the program again. The test.dat file will still be opened to save the file locally.

main

In the text, I will present the source code and explain it in part in the form of comments. This address book is a good summary of the previous knowledge. Shu Wen suggests that everyone follow along. 😁

test.c section

Mainly implement code logic, etc.:

#include "contact.h"
void menu()
{
    printf("************************************\n");
    printf("*****1.ADD 2.DEL 3.SEARCH****\n");
    printf("*****4.MODIFY 5.SHOW 6.SORT******\n");
    printf("*****7.CLEAR_EVERY 0.EXIT ******\n");
    printf("************************************\n");
    printf("************************************\n");
}
enum Opinion
{
    EXIT,
    ADD,
    DEL,
    SEARCH,
    MODIFY,
    SHOW,
    SORT,
    CLEAR_EVERY,
};
int main()
{
    contact con = { 0 };
    // structure initialization
    InitContact(&con);
    // function implementation
    int input = 0;

  do
  {
    menu();
    printf("Please enter the desired function:>");
    scanf("%d", &input);
    switch(input)//Address book function
  {
    case ADD:
        AddContact(&con);
        break;
    case SORT:
      SortContact(&con);
      break;
    case DEL:
      DelContact(&con);
      break;
    case CLEAR_EVERY:
      Clear_Every(&con);
      break;
    case SHOW:
      ShowContact(&con);
      break;
    case MODIFY:
      ModifyContact(&con);
      break;
    case SEARCH:
      SearchContact(&con);
      break;
    case EXIT:
      SaveContact(&con);//Save the information to the file, close the file and NULL the file pointer
      DestoryContact(&con);//Dynamic pointer NULL free dynamic memory
      printf("Exit successfully");
      break;
    default:
      printf("Input error\n");
      break;
  }
  } while (input);
return 0;
}

contact.c section

This part of the content is mainly about the implementation of functions, and the source code is sent to everyone. Personally, I feel that this is not difficult. (Of course, there are also lazy aspects of bloggers)

#include "contact.h"
void LoadContact(contact*pc)
{
  FILE* p = fopen("contact.dat", "r");
  if (p == NULL)
  {
    perror("LoadContact");
    return;
  }
  PeoInfo tmp = { 0 };//This temporary variable is to adjust the order.
  //Of course, this temporary variable can also be omitted by adjusting the position of pc->sz++.
  while (fread(&tmp, sizeof(PeoInfo), 1, p))
  {
    CheckContact(pc);
    pc->date[pc->sz] = tmp;
    pc->sz++;
  }
  fclose(p);
  p = NULL;
}
void InitContact(contact*pc)
{
  assert(pc);
  pc->date = (PeoInfo*)calloc(CAPACITY_SZ, sizeof(PeoInfo));
  if (pc->date == NULL)
{
  perror("InitContact");
  return;
}
pc->capacity = CAPACITY_SZ;
pc->sz = 0;
LoadContact(pc);
}
void CheckContact(contact*pc)
{
if (pc->sz == pc->capacity)
{
PeoInfo* ptr = realloc(pc->date, sizeof(PeoInfo) * (pc->capacity + ADD_SZ));
if (ptr != NULL)
{
pc->date = ptr;
printf("Successful expansion\n");
pc->capacity += ADD_SZ;
}
else
{
printf("Capacity failed");
perror("CheckContact");
}
}
}
void AddContact(contact* pc)//Increase the number of people
{
CheckContact(pc);//Check whether the address book has been occupied and increase the occupancy
printf("Please enter your name:>\n");
scanf("%s", pc->date[pc->sz].name);
printf("Please enter the phone number\n");
scanf("%s", pc->date[pc->sz].tel);
printf("Please input age:>\n");
scanf("%d", &pc->date[pc->sz].age);
printf("Please enter gender>:\n");
scanf("%s", pc->date[pc->sz].sex);
printf("Please enter the address>:\n");
scanf("%s", pc->date[pc->sz].address);
(pc->sz)++;
printf("Add success\n");
}
void ShowContact(contact*pc)
{
int i = 0;
//print title
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\n", "name", "age", "sex", "phone", "address") ;
// print data
for (i = 0; i < pc->sz; i++)
{
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-20s\n",
pc->date[i].name,
pc->date[i].age,
pc->date[i].sex,
pc->date[i].tel,
pc->date[i].address);
}
}
int cmp(const void* p1,const void* p2)
{
return (strcmp(((PeoInfo*)p1)->name, ((PeoInfo*)p2)->name));
}
void SortContact(contact*pc)
{
qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp);
printf("Successful sorting\n");
ShowContact(pc);
}
int FindContact(contact* pc, char* name)
{
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(pc->date[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void DelContact(contact*pc)
{
char name[NAME_MAX] = { 0 };
assert(pc);
if (pc->sz == 0)
{
printf("Sister, no one can delete it");
return;
}
printf("Please enter the name of the person you want to delete>:");
scanf("%s", name);
int pos = FindContact(pc,name);
if (-1 == pos)
{
printf("Contact does not exist\n");
return;
}
int i = 0;
for (i = pos; i < pc->sz; i++)
{
pc->date[i] = pc->date[i + 1];
}
pc->sz--;
printf("Deleted successfully\n");
}
void Clear_Every(contact* pc)
{
assert(pc);
printf("Are you sure?\nEnter 1 to confirm your choice\n");
int input = 0;
scanf("%d", &input);
if (1 == input)
{
pc->date = (PeoInfo*)calloc(CAPACITY_SZ, sizeof(PeoInfo));
if (pc->date == NULL)
{
perror("Clear_Every");
return;
}
pc->capacity = CAPACITY_SZ;
pc->sz = 0;
printf("Clear success");
}
else
{
printf("Your contact is still there\n");
return;
}
}

void ModifyContact(contact*pc)
{
printf("Please enter the name of the person to be changed>:\n");
char name[NAME_MAX] = { 0 };
scanf("%s", name);
int por = FindContact(pc, name);
if (-1 == por)
{
printf("Contact does not exist\n");
return;
}
printf("Please re-enter the changed information\n");
printf("Please enter your name:>\n");
scanf("%s", pc->date[por].name);
printf("Please enter the phone number\n");
scanf("%s", pc->date[por].tel);
printf("Please input age:>\n");
scanf("%d", &pc->date[por].age);
printf("Please enter gender>:\n");
scanf("%s", pc->date[por].sex);
printf("Please enter the address>:\n");
scanf("%s", pc->date[por].address);
(pc->sz)++;
printf("Successful change\n");
}
void SearchContact(contact*pc)
{
char name[NAME_MAX] = { 0 };
printf("Please enter the name of the person you are looking for");
scanf("%s", name);
int por = FindContact(pc, name);
if (-1 == por)
{
printf("No contact found\n");
return;
}
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\n", "name", "age", "sex", "phone", "address") ;
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-20s\n", pc->date[por].name
, pc->date[por].age,
pc->date[por].sex,
pc->date[por].tel,
pc->date[por].address);
}
void DestoryContact(contact*pc)
{
free(pc->date);
pc->date = NULL;
pc->sz = 0;
pc->capacity = 0;
}
void SaveContact(contact*pc)
{
FILE* p = fopen("contact.dat", "w");
if (p == NULL)
{
perror("SaveContact");
return;
}
int i = 0;
for (i = 0; i < pc->sz; i++)
{
fwrite(pc->date + i, sizeof(PeoInfo), 1, p);
}
fclose(p);
p = NULL;
}

contact.h section

This part is just about the function declaration and the creation of the structure.

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define NAME_MAX 20
#define SEX_MAX 10
#define TEL_MAX 20
#define ADRESS_MAX 30
#define PEOPLE_MAX 5
#define CAPACITY_SZ 5
#define ADD_SZ 3
//Name, gender, age, phone, address
typedef struct PeoInfo
{
char name[NAME_MAX];
char sex[SEX_MAX];
int age;
char tel[TEL_MAX];
char address[ADRESS_MAX];
}PeoInfo;//Store a person's structure
typedef struct contact
{
PeoInfo* date;//Dynamic management requires the use of pointers
int sz;//Number of valid persons
int capacity;//for storage capacity
}contact;
void InitContact(contact* pc);//Initialize contact
void AddContact(contact* pc);//The realization of ADD function
void ShowContact(contact* pc);//The realization of SHOW function
void SortContact(contact* pc);//Implementation of sorting function
void DelContact(contact* pc);//The realization of delete function
void Clear_Every(contact* pc);//Clear everyone's implementation
void DestoryContact(contact* pc);//NULL for opening up space
void SearchContact(contact* pc);//Find personnel function implementation
void ModifyContact(contact* pc);//Change the implementation of personnel
void SaveContact(contact* pc);//Save the address book
void CheckContact(contact* pc);//Check the address book space and achieve capacity expansion
created at:02-08-2022
edited at: 02-08-2022: