How to realize the File version address Book in programming Development
Editor to share with you how to implement the file version of the address book in programming development, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it!
Source code:
"Contact.h"
# ifndef _ CONTACT_H__#define _ CONTACT_H__#include # include # includeenum OP {EXIT, ADD, DELE, SER, MODIFY, DISPLAY, CLEAR, SORT}; # define NAME_MAX 20#define SEX_MAX 5#define TELE_MAX 12#define ADDR_MAX 25#define MAX 1000#define DEFAULT 2 / / default size of the address book for the first time # define MAX_INIT 2typedef struct PerInfo {char name [name _ MAX] Int age;char sexes [SEX _ MAX]; char Telegraph [telle _ MAX]; char addr [ADDR _ MAX];} PeoInfo;typedef struct Contact {PeoInfo * data;int size;int iint temp;int capacity;} Contact,*Pcon;void init (Pcon pcon); void _ add (Pcon pcon); void _ display (Pcon pcon); void _ del (Pcon pcon); void _ search (Pcon pcon); void _ modify (Pcon pcon); void _ sort (Pcon pcon); void _ clear (Pcon pcon); void _ destory (Pcon pcon) Void _ save (Pcon pcon); void _ load (Pcon pcon); # endif / / _ CONTACT_H__
"Contact.c"
# include "Contact.h" void init (Pcon pcon) {pcon- > size = 0 PeoInfo Pcon-> capacity = DEFAULT; pcon- > data= (PeoInfo *) malloc (MAX_INIT*sizeof (PeoInfo)); if (pcon- > data= = NULL) {printf ("out of menory"); exit (EXIT_FAILURE);} memset (pcon- > data,0,pcon- > capacity*sizeof (PeoInfo)); _ load (pcon) } void _ rise (Pcon pcon) / / increase capacity {PeoInfo * temp = (PeoInfo *) realloc (pcon- > data, (pcon- > capacity+MAX_INIT) * sizeof (PeoInfo)); if (temp = = NULL) {printf ("out of memory!\ n"); exit (0);} else pcon- > data = temp } void _ dela (Pcon pcon) / / reduce capacity {PeoInfo * temp = (PeoInfo *) realloc (pcon- > data, (pcon- > capacity-MAX_INIT) * sizeof (PeoInfo)); if (temp = = NULL) {printf ("phone book is empty! \ n "); return;} else pcon- > data = temp;} void _ destory (Pcon pcon) {if (pcon- > data! = NULL) {free (pcon- > data); pcon- > data = NULL;} void _ add (Pcon pcon) {PeoInfo * temp = NULL; if (pcon- > size = = pcon- > capacity) {_ rise (pcon) } printf ("Please enter name: >"); scanf ("% s", pcon- > data [Pcon-> size] .name); printf ("Please enter age: >"); scanf ("% d", & (pcon- > data [Pcon-> size] .age)); printf ("Please enter gender: >"); scanf ("% s", pcon- > data [Pcon-> size] .sex); printf ("Please enter phone number: >") Scanf ("% s", pcon- > data [Pcon-> size] .tele); printf ("Please enter address: >"); scanf ("% s", pcon- > data [Pcon-> size] .addr); pcon- > size++;printf ("added successfully\ n");} void _ display (Pcon pcon) {int I = 0scape printf ("% 9s\ t% 3s\ t% 4s\ ts\ ts\ n", "name", "age", "sex", "tele", "addr"); for (I = 0th isize) Ts +) {printf ("% 9s\ t% 3D\ t% 4s\ ts\ ts\ n", pcon- > data [I] .name, pcon- > data [I] .age, pcon- > data [I] .sex, pcon- > data [I] .tele, pcon- > data [I] .addr)}} static int find (Pcon pcon, char * name) {if (strcmp (pcon- > DataI [.name, name) = 0) {return;}} return-1 } void _ del (Pcon pcon) {int pos = 0tint index = 0Tracchar name [name _ MAX]; if (pcon- > size = = 0) {printf ("phone is empty\ n"); return;} printf ("Please enter the person's name to delete: >"); scanf ("% s", name); pos = find (pcon, name); if (pos =-1) {printf ("cannot find the person to delete\ n"); return;} / for (index = pos; index)
< pcon->Size; index++) {pcon- > data [index] = pcon- > data [index+1];} pcon- > size--;printf ("deleted successfully\ n"); _ dela (pcon);} void _ search (Pcon pcon) {char name [name _ MAX] = {0}; int pos = 0ash printf ("Please enter the name of the person you are looking for: >"); scanf ("% s", name); pos = find (pcon, name); if (pos =-1) {printf ("specified contact does not exist\ n") Return;} else {printf ("% 9s\ t% 3s\ t% 4s\ ts\ ts\ n", "name", "age", "sex", "tele", "addr"); printf ("% 9s\ t% 3D\ t% 4s\ ts\ ts\ n", pcon- > datapos] .name, pcon- > datapos .age, pcon- > datapos .sex, pcon- > data [pos] .tele, pcon- > data [pos] .addr) }} void _ modify (Pcon pcon) {char name [name _ MAX] = {0}; int pos = 0politics printf ("Please enter the name of the person to be modified: >"); scanf ("% s", name); pos = find (pcon, name); if (pos = =-1) {printf ("specified contact does not exist\ n"); return;} else {printf ("Please enter name: >"); scanf ("% s", pcon- > data [pos] .name) Printf ("Please enter age: >"); scanf ("% d", & (pcon- > data [pos] .age)); printf ("Please enter gender: >"); scanf ("% s", pcon- > datapos [sex] .sex); printf ("Please enter phone number: >"); scanf ("% s", pcon- > datapos] .tele); printf ("Please enter address: >"); scanf ("% s", pcon- > datapos] .addr) }} void _ sort (Pcon pcon) {temp = pcon- > data [j]; pcon- > data [j] = pcon- > data [juni1]; for (I = 0 = 0; jsize-1-i; +) / / controls the number of sorted trips {for (j = 0; jsize-1-i; jacks +) {if (strcmp (pcon- > Data [j] .name, pcon- > dataj + 1] .name) > 0) {temp = pcon- > people [j]; pcon- > data [j] = pcon- > data [jung1]; pcon- > data [jarg1] = temp } void _ clear (Pcon pcon) {pcon- > size = 0;} void _ save (Pcon pcon) / / write file {int I = 0; FILE * pfWrite = fopen ("contact.txt", "w"); / / Open a file if (pfWrite = NULL) {perror ("open file for write"); exit (0) } else {for (iTuno; isize; iTunes +) {fwrite (& (pcon- > data [I]), sizeof (PeoInfo), 1je pfWrite);} fclose (pfWrite);}} void _ load (Pcon pcon) / / load file contents {int iTun0; PeoInfo temp = {0} FILE * pfRead = fopen ("contact.txt", "r"); if (pfRead = = NULL) {perror ("open file for read"); exit (0);} while (fread (& temp,sizeof (PeoInfo), 1dfRead)) {_ rise (pcon); pcon- > data [I] = temp; iTunes; pcon- > size++ }}
"test.c"
# include "Contact.h" void menu () {printf ("* *\ n"); printf ("* 1.add *\ n"); printf ("* 2.del *\ n") Printf ("* 3.search *\ n"); printf ("* 4.modify *\ n"); printf ("* 5.display *\ n"); printf ("* 6.clear *\ n") Printf ("* 7.sort *\ n"); printf ("* 0.exit *\ n"); printf ("* *\ n");} int main () {int input = 1 * contact pcon;init (& pcon); while (input) {menu () Printf ("Please select the number to operate:"); scanf ("% d", & input); printf ("\ n"); {switch (input) {case ADD:_add (& pcon); break;case DELE:_del (& pcon); break;case SER:_search (& pcon); break;case MODIFY:_modify (& pcon); break;case DISPLAY:_display (& pcon); break;case CLEAR:_clear (& pcon); break;case SORT:_sort (& pcon); break Case EXIT:_save (& pcon); _ destory (& pcon); break;default:printf ("invalid choice!"); break;} system ("pause"); return 0;} these are all the contents of the article "how to implement a file version address book in programming development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!