Contact.h
#ifndef __contact_h__#define __CONTACT_H__#include <stdlib.h>#include <stdio.h>#define Name_len#define Sex_len 5#define Tele_len#define Addr_len#define MAX_PEOtypedefstructpeo{CharName[name_len];intAgeCharSex[sex_len];CharTele[tele_len];CharAddr[addr_len];} Peo,*ppeo;typedefstructcontact{PEODHB[MAX_PEO];//Store person's information intCount//Record effective number}pcon,*pcon;voidAdd_peo (Pcon Pcon);voidDel_peo (Pcon Pcon);voidSearch (Pcon Pcon);voidModify (Pcon Pcon);voidShow (Pcon Pcon);voidClear (Pcon Pcon);voidInit (Pcon Pcon);voidSave (Pcon Pcon);#endif
Text.c
#include <stdio.h>#include "contact.h"voidPrint_menu () {printf("*************************************\n");printf("*****1:add 2:del******\n");printf("*****3:search 4:modify***\n");printf("*****5:show 6:clear****\n");printf("************* 7:save ****************\n");printf("*************************************\n");}intMain () {PCON con;intinput =1; Con.count =0; Init (&con); while(input) {Print_menu ();printf("Please select operation: \ n");scanf("%d", &input);Switch(input) { Case 1: Add_peo (&con); Break; Case 2: Del_peo (&con); Break; Case 3: Search (&con); Break; Case 4: Modify (&con); Break; Case 5: Show (&con); Break; Case 6: Clear (&con); Break; Case 7: Save (&con); Break;default: Break; } }return 0;}
Main.c
#include <stdio.h>#include <string.h>#include "contact.h"intFindChar*p, Pcon Pcon) {inti =0; for(; i < pcon->count; i++) {if(strcmp(pcon->dhb[i].name,p) = =0)returnI }return-1;}voidAdd_peo (Pcon Pcon) {if(Pcon->count < MAX_PEO) {printf("Please enter your name, age, gender, phone number, address: \ n");scanf('%s ', PCON->DHB [Pcon->count].name);scanf("%d", &PCON->DHB [Pcon->count].age);scanf('%s ', PCON->DHB [Pcon->count].sex);scanf('%s ', PCON->DHB [Pcon->count].tele);scanf('%s ', PCON->DHB [pcon->count].addr); } Pcon->count + +;printf("Storage succeeded \ n");}voidDel_peo (Pcon Pcon) {inti =0;intRET =0;Charname[Ten];printf("Please enter the name you want to delete: \ n");scanf('%s ', name); ret = find (Name,pcon);if(ret = =-1) {printf("no contact \ n"); }Else for(i = ret; I < pcon->count-1; i++) {Pcon->dhb[i] = pcon->dhb[i +1]; } pcon->count--;printf("Delete succeeded \ n");}voidSearch (Pcon Pcon) {inti =0;intRET =0;Charname[Ten];printf("Please enter the name of the person you want to find: \ n");scanf('%s ', name); ret = find (Name,pcon);if(ret = =-1) {printf("no contact \ n"); }Else{printf("Name:%10s\t", pcon->dhb[i].name);printf("Age:%10d\t", pcon->dhb[i].age);printf("Gender:%10s\t", pcon->dhb[i].sex);printf("Tel:%10s\t", Pcon->dhb[i].tele);printf("Address:%10s\t", PCON->DHB[I].ADDR);printf("\ n"); }}voidModify (Pcon Pcon) {inti =0;intRET =0;Charname[Ten];printf("Please enter the name of the person you want to find: \ n");scanf('%s ', name); ret = find (Name,pcon);printf("Please enter your name, age, gender, phone number, address: \ n");scanf('%s ', PCON->DHB [I].name);scanf("%d", &PCON->DHB [I].age);scanf('%s ', PCON->DHB [I].sex);scanf('%s ', PCON->DHB [I].tele);scanf('%s ', PCON->DHB [i].addr);printf("modification succeeded \ n");}voidShow (Pcon Pcon) {inti =0; for(; I <pcon->count; i++) {printf("Name:%10s\t", pcon->dhb[i].name);printf("Age:%10d\t", pcon->dhb[i].age);printf("Gender:%10s\t", pcon->dhb[i].sex);printf("Tel:%10s\t", Pcon->dhb[i].tele);printf("Address:%10s\t", PCON->DHB[I].ADDR);printf("\ n"); }}voidClear (Pcon Pcon) {Pcon->count =0;}voidInit (Pcon Pcon) {inti =0; FILE *pfread = fopen ("Contact.txt","R");if(Pfread = = NULL) {Perror ("Contact.txt");Exit(exit_failure); } while(Fread (& (Pcon->dhb[i)),sizeof(PEO),1, pfread) = =1) {i++; } pcon->count = i; Fclose (Pfread);}voidSave (Pcon Pcon) {inti =0; FILE *pfwrite = fopen ("Contact.txt","W");if(Pfwrite = = NULL) {Perror ("Contact.txt");Exit(exit_failure); } for(i=0; i<pcon->count;i++) {fwrite (& (Pcon->dhb[i]),sizeof(PEO),1, Pfwrite); } fclose (Pfwrite);}
Implement an address book and save the information to text