The address book has been improved based on Address Book (1) and added the ability to save contacts.
Important knowledge points of C language are:
File stream operations
Code:
Main. c
# Include <stdio. h> # include "record. H "int menu_select (void); void hand_menu (INT cmd, int * flag); int main (INT argc, char * argv []) {int cmd = 0; int flag = 1; while (1) {cmd = menu_select (); If (cmd = '0') return 0; hand_menu (CMD, & flag );}} int menu_select (void) {int select; printf ("<------ communication thin --------> \ n"); printf ("1: add contact 2: Delete contact \ n "); printf ("3: show all contacts 4: Save \ n"); printf ("0: Exit \ n"); printf ("Enter: \ n "); select = getch (); While (select <'0' | SELECT> '4') {printf ("input error, please input again: \ n "); select = getch ();} return select;} void hand_menu (INT cmd, int * flag) {static ADDR * list_head = NULL; if (1 = * flag) {list_head = init_person (list_head); * flag = 0;} switch (CMD) {Case '1': list_head = add_person (list_head); break; Case '2 ': list_head = del_person (list_head); break; Case '3': dis_person (list_head); break; Case '4': save_person (list_head); break; default: break ;}}
Record. h
#ifndef _RECORD_H_#define _RECORD_H_typedef struct{char name[8];char tel[20];}DATA;typedef struct node{DATA data;struct node *next;}ADDR;#define ASK(p) do{\p = (ADDR *)malloc(sizeof(ADDR));\if(p==NULL){printf("malloc memory failed!");exit(-1);}\}while(0)#endif
Opre. c
# Include <stdio. h> # include "record. H "# define file_name" phonebook. dat "ADDR * add_person (ADDR * list_head) {ADDR * head = list_head; ADDR * node = list_head; ADDR * new_p; ask (new_p); new_p-> next = NULL; printf ("Enter name:"); scanf ("% s", new_p-> data. name); printf ("Enter the phone number:"); scanf ("% s", new_p-> data. tel); If (! Node) {head = new_p; return head;} while (node-> next) node = node-> next; node-> next = new_p; return head ;} ADDR * del_person (ADDR * list_head) {ADDR * node = list_head; ADDR * head = list_head; char name [8]; ADDR * pre = node; printf ("Enter the name to delete:"); scanf ("% s", name); If (! Strcmp (Head-> data. name, name) {pre = head; head = head-> next; free (pre); return head;} while (node) {If (! Strcmp (node-> data. Name, name) {pre-> next = node-> next; free (node); printf ("deleted successfully! \ N "); return;} Pre = node; node = node-> next;} printf (" this name is not found! \ N "); return head;} void dis_person (ADDR * list_head) {ADDR * node = list_head; If (! Node) return; printf ("name number \ n"); While (node) {printf ("% S % s \ n", node-> data. name, node-> data. tel); node = node-> next ;}} void save_person (ADDR * list_head) {file * PF; ADDR * node = list_head; pF = fopen (file_name, "W +"); While (node) {fprintf (PF, "% S % s \ n", node-> data. name, node-> data. tel); node = node-> next;} fclose (PF); printf ("saved successfully! \ N ");} ADDR * init_person (ADDR * list_head) {ADDR * node = list_head; ADDR * head = list_head; ADDR * new_node; file * PF; char name [8]; char Tel [20]; ask (new_node); pF = fopen (file_name, "R"); If (! Fscanf (PF, "% S % s", new_node-> data. name, new_node-> data. tel) {free (new_node); return head;} rewind (PF); While (fscanf (PF, "% S % s", name, TEL) = 2) {ask (new_node); new_node-> next = NULL; strcpy (new_node-> data. name, name); strcpy (new_node-> data. tel, TEL); If (! Head) node = head = new_node; else {While (node-> next) node = node-> next; node-> next = new_node ;}} return head ;}