Implement an address book and save the information to text

Source: Internet
Author: User

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-&GT;DHB [Pcon->count].name);scanf("%d", &AMP;PCON-&GT;DHB [Pcon->count].age);scanf('%s ', PCON-&GT;DHB [Pcon->count].sex);scanf('%s ', PCON-&GT;DHB [Pcon->count].tele);scanf('%s ', PCON-&GT;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-&GT;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-&GT;DHB [I].name);scanf("%d", &AMP;PCON-&GT;DHB [I].age);scanf('%s ', PCON-&GT;DHB [I].sex);scanf('%s ', PCON-&GT;DHB [I].tele);scanf('%s ', PCON-&GT;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-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.