Write a text file to the c-language Address Book (including reading, deleting, and modifying a line)

Source: Internet
Author: User

Write a text file to the c-language Address Book (including reading, deleting, and modifying a line)

/* Implement an address book. The address book can be used to store information of 1000 people. Each person's information includes: name, gender, age, phone number, and address. add Contact Information 2. delete the specified contact information. 3. search for the specified contact information. 4. modify the specified contact information. show all contacts 6. clear all contacts 7. display Text Phone Book information * // header file # ifndef _ PHONE_BOOK_H __# define _ PHONE_BOOK_H _ typedef struct phone_book {char name [10]; char sex [5]; char addr [20]; int year; char phone_num [12]; struct phone_book * pNext;} Book, * pBook; pBook add_linkman (pBook head); pBook remove_linkman (pBook head ); void Traver_linkman (pBook head); pBook empty_linkman (pBook head); pBook amend_linkman (pBook head); void search_linkman (pBook head); void start (pBook * head ); void write_file (pBook position); // write the text void read_file (pBook head); // read the text information void amend_file (pBook position, char * name, int flag ); // modify and delete a line of information in the text # endif // function file # include <stdio. h> # include "phone_book.h" # include <string. h> # include <assert. h> extern char file_name [30] = "\ 0"; void start (pBook * phead) {int I =-1; assert (phead); while (1) {printf ("welcome to the phone book system! Select your operation: \ n "); printf ("************************************* * ******* \ n "" * 1. add Contact Information 2. delete the specified contact information * \ n "" * 3. search for the specified contact information. 4. modify the specified contact information * \ n "" * 5. show all contacts 6. clear all contacts * \ n "" * 7. display phone text information 0. end the system! * \ N "" * ---- ---- * \ n ""************************* * ******************* \ n "); scanf ("% d", & I); switch (I) {case 0: printf ("***** Thank you for using this system! * *** \ N "); printf (" the storage path of your phone book is % s. Do not forget it! \ N ", file_name); exit (0); case 1: * phead = add_linkman (phead); break; case 2: * phead = remove_linkman (phead); break; case 3: search_linkman (phead); break; case 4: * phead = amend_linkman (phead); break; case 5: traver_linkman (phead); break; case 6: empty_linkman (phead); break; case 7: read_file (phead); break; default: printf ("Incorrect choice! Please reselect \ n "); break ;}} pBook add_linkman (pBook head) {int I = 0; char name [10]; char sex [5]; char addr [20]; int year; long phone_num; pBook pBase = head; assert (head); flag: printf ("How many contacts do you want to add :"); scanf ("% d", & I); if (I <= 0) {printf ("incorrect number of inputs! \ N "); goto flag;} if (head-> pNext = NULL) {while (I --) {pBook pNew = (pBook) malloc (sizeof (Book )); assert (pNew); printf ("Enter the contact information (name, gender, address, age, phone number) \ n"); scanf ("% s", name ); scanf ("% s", sex); scanf ("% s", addr); strcpy (pNew-> name, name); strcpy (pNew-> sex, sex ); strcpy (pNew-> addr, addr); scanf ("% d", & pNew-> year); scanf ("% s", & pNew-> phone_num ); pBase-> pNext = pNew; pNew-> pNext = NULL; pBase = pNew; Write_file (pNew) ;}} else {while (pBase-> pNext! = NULL) {pBase = pBase-> pNext;} while (I --) {pBook pNew = (pBook) malloc (sizeof (Book); assert (pNew ); printf ("Enter the contact information (name, gender, address, age, phone number) \ n"); scanf ("% s", name ); scanf ("% s", sex); scanf ("% s", addr); strcpy (pNew-> name, name); strcpy (pNew-> sex, sex ); strcpy (pNew-> addr, addr); scanf ("% d", & pNew-> year); scanf ("% s", & pNew-> phone_num ); pBase-> pNext = pNew; pNew-> pNext = NULL; pBase = pNew; write_file (p New) ;}} return head;} pBook remove_linkman (pBook head) {pBook link_man = head; char name [10]; if (! Head) {printf ("no contact, cannot be deleted! \ N "); return NULL;} printf (" which contact do you want to delete? "); Scanf (" % s ", name); while (link_man-> pNext! = NULL) {if (strcmp (link_man-> pNext-> name, name) = 0) {amend_file (link_man-> pNext, name, 0 ); if (link_man-> pNext = NULL) {free (link_man-> pNext); link_man-> pNext = NULL; printf ("deletion completed! \ N ") ;}else {pBook ptmp = NULL; ptmp = link_man-> pNext; link_man-> pNext = link_man-> pNext; free (ptmp ); printf ("deleted! \ N ") ;}return head ;}else {link_man = link_man-> pNext ;}} printf (" this contact is not found! \ N "); return head;} void traver_linkman (pBook head) {pBook link_man = head; if (head-> pNext = NULL) {printf (" the phone book is empty! \ N ");} while (link_man-> pNext! = NULL) {printf ("% s \ n", link_man-> pNext-> name); printf ("% s \ n", link_man-> pNext-> sex ); printf ("% s \ n", link_man-> pNext-> addr); printf ("% d \ n", link_man-> pNext-> year ); printf ("% s \ n", link_man-> pNext-> phone_num ); printf ("********************************** \ n "); link_man = link_man-> pNext ;}} pBook empty_linkman (pBook head) {pBook ptmp = NULL; pBook link_man = head; if (head-> pNext = NULL) {printf ("the phone book is Null! No need to clear it! (Defaults 0 rows) \ n "); return NULL;} while (link_man-> pNext! = NULL) {if (link_man-> pNext = NULL) {free (link_man-> pNext); link_man-> pNext = NULL ;} else {ptmp = link_man-> pNext; link_man-> pNext = link_man-> pNext; free (ptmp) ;}} printf ("cleared! \ N "); write_file (NULL); return NULL;} pBook amend_linkman (pBook head) {char name [10]; char sex [5]; char addr [20]; pBook link_man = head; if (! Head) {printf ("no contact, cannot be modified! \ N "); return NULL;} printf (" which contact Do You Want To modify? "); Scanf (" % s ", name); while (link_man-> pNext! = NULL) {if (strcmp (link_man-> pNext-> name, name) = 0) {printf ("Enter the contact information (name, gender, address, age, phone number) \ n"); scanf ("% s", name ); scanf ("% s", sex); scanf ("% s", addr); strcpy (link_man-> pNext-> name, name ); strcpy (link_man-> pNext-> sex, sex); strcpy (link_man-> pNext-> addr, addr); scanf ("% d ", & link_man-> pNext-> year); scanf ("% s", & link_man-> pNext-> phone_num); amend_file (link_man-> pNext, name, 1 ); return head;} else li Nk_man = link_man-> pNext;} return head;} void search_linkman (pBook head) {char name [10]; pBook link_man = head; if (! Head) {printf ("no contact, search failed! \ N "); return NULL;} printf (" which contact are you looking? "); Scanf (" % s ", name); while (link_man-> pNext! = NULL) {if (strcmp (link_man-> pNext-> name, name) = 0) {printf ("% s \ n ", link_man-> pNext-> name); printf ("% s \ n", link_man-> pNext-> sex); printf ("% s \ n ", link_man-> pNext-> addr); printf ("% d \ n", link_man-> pNext-> year); printf ("% s \ n ", link_man-> pNext-> phone_num ); printf ("********************************** \ n "); return 0;} else link_man = link_man-> pNext;} printf ("not found! \ N ");} void write_file (pBook position) {FILE * list; if (position! = NULL) {if (strcmp (file_name, "\ 0") = 0) {printf ("Enter the file path you want to store :"); scanf ("% s", file_name); list = fopen (file_name, "wt"); fprintf (list, "%-10 s", "name "); fprintf (list, "%-6 s", "gender"); fprintf (list, "%-10 s", "Address"); fprintf (list, "%-8 s", "Age"); fprintf (list, "%-20 s", "Number"); fprintf (list, "% s ", "\ n");} else list = fopen (file_name, "a +"); fprintf (list, "%-10 s", position-> name ); fprintf (list, "%-6 s", posit Ion-> sex); fprintf (list, "%-10 s", position-> addr); fprintf (list, "%-8d", position-> year ); fprintf (list, "%-20 s", position-> phone_num); fprintf (list, "% s", "\ n ");} else {list = fopen (file_name, "w"); fprintf (list, "% s", "destroyed Address Book"); strcpy (file_name, "\ 0") ;}fclose (list);} void read_file (pBook head) {if (head-> pNext = NULL) {printf ("Empty Address Book \ n");} else {FILE * list = fopen (file_name, "r"); cha R ch; while (ch = fgetc (list ))! = EOF) {putchar (ch) ;}} void amend_file (pBook position, char * name, int I) {char str [100]; long num1 = 0; FILE * list = fopen (file_name, "r"); while (fgets (str, 100, list )! = NULL) {if (strstr (str, name) = NULL) {num1 + = strlen (str) + 1;} else break;} fclose (list ); list = fopen (file_name, "r +"); fseek (list, num1, SEEK_SET); if (I = 0) {fprintf (list, "% 54 s ", ""); fprintf (list, "% s", "\ n");} else {fprintf (list, "%-10 s", position-> name ); fprintf (list, "%-6 s", position-> sex); fprintf (list, "%-10 s", position-> addr); fprintf (list, "%-8d", position-> year); fprintf (list, "%-20 s", position-> phone_num); fprintf (list, "% s ", "\ n") ;}fclose (list) ;}// main function # include <stdio. h> # include "phone_book.h" char file_name [30]; int main () {pBook phead = NULL; start (& phead); return 0 ;}

 

Related Article

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.