C language Address Book write text file (with read, delete a row, modify a row)

Source: Internet
Author: User
Tags assert strcmp

Tag: c; Read the file; Modify a line in the text; list

/*    implements an address book that can be used to store information about 1000 people, including name, gender, age, phone number, address   How to provide:1.   add contact information 2.    Delete specified contact information 3.   Find specified contact information 4.   Modify the specified contact information 5.   Show all contact information 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);p Book remove_linkman (pbook head);void  Traver_linkman (Pbook head);p Book empty_linkman (pbook head);p Book amend_linkman (pBook  Head); Void search_linkman (Pbook head); Void start (Pbook *head); Void write_file (PBook  position);//write Text void read_file (pbook head);//Read text information void amend_file (pbook position,  char *name, int flag);//modify, delete a line of information in the text #endif//the function file #include<stdio.h> #include "phone_book.h" #include <string.h> #include <assert.h>extern char file_ Name[30]= "Void start" (pbook *phead) {Int i = -1;assert (Phead);while  (1) {printf ("Welcome to the phone book system!") Select your action: \ n ");p rintf (" *********************************************\n "   " * * ". Add contact information       2. Delete the specified contact information   *\n "   ". Find the specified contact information  4. Modify the specified contact information    *\n "   ". Show all contact information  6. Clear all Contacts       *\n "     "*7. Display phone text inside information  0. End System!           *\n "   " *          ———— CIA-specific ————          *\n "                 "******************************* \ n "); scanf ("%d ", &i);switch  (i) {case 0:printf (" * * * * * Thank you for using this system!*****\n ");p rintf ("   your phone book storage path for:%s   don't forget Oh! \ 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 ("Wrong choice!") Please re-select \ n "); 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 ("Wrong number of inputs!"). \ n "); goto flag;} if  (head->pnext == null) {while  (i--) {pbook pnew =  (pBook) malloc (sizeof ( (pnew);p rintf ("Please Enter contact information: (Name, gender, address, age, phone number) \ n"), scanf ("%s",  name); scanf ("%s", scanf ("%s",  addr), strcpy (Pnew->name, name); strcpy (pnew->sex, sex); strcpy (pnew-> ADDR,&NBSP;ADDR); scanf ("%d",  &pnew->year), scanf ("%s",  &pnew->phone_num);p base-> 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), assert (Pnew);p rintf (Enter 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-&GT;ADDR,&NBSP;ADDR); scanf ("%d", &pnew-> year); scanf ("%s",  &pnew->phone_num);p base->pnext = pnew;pnew->pnext =  Null;pbase = pnew;write_file (pnew);}} Return head;} Pbook remove_linkman (Pbook head) {pbook link_man = head;char name[10];if (!head) {printf ("No contacts, can't delete! \ n "); return null;} printf ("which contact would you like 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->pnext  == null) {free (link_man->pnext); link_man->pnext = null;printf ("Delete complete! \ n ");} Else{pbook ptmp = null;ptmp = link_man->pnext;link_man->pnext = link_ Man->pnext->pnext;free (ptmp);p rintf ("Delete done! \ n ");} Return head;} Else{link_man = link_man->pnext;}} printf ("The contact was not found!") \ n "); return head;} Void traver_linkman (Pbook head) {pbook link_man = head;if  (head->pNext ==  null) {printf ("Phone book is empty! \ n ");} while  (link_man->pnext!=null) {printf ("%s\n",  link_man->pnext->name);p rintf ("%s\n",  Link_man->pnext->sex);p rintf ("%s\n",  link_man->pnext->addr);p rintf ("%d\n", &nbsP;link_man->pnext->year);p rintf ("%s\n",  link_man->pnext->phone_num);p rintf ("***************** \ n "); link_man = link_man->pnext;}} Pbook empty_linkman (Pbook head) {pbook ptmp = null;pbook link_man =  head;if  (head->pnext == null) {printf ("Phone book is empty! Don't empty it! (ˇ0ˇ)  \n "); return null;} while  (link_man->pnext != null) {if  (link_man->pnext->pnext == null) { Free (link_man->pnext); link_man->pnext = null;} Else{ptmp = link_man->pnext;link_man->pnext = link_man->pnext->pnext;free (ptmp );}} printf ("Empty finished!" \ n "); Write_file (NULL); return null;} Pbook amend_linkman (Pbook head) {char name[10];char sex[5];char addr[20];p book  link_man = head;if  (!head) {printf ("No contacts, cannot be modified!") \ n "); return null;} printf ("Would you like to change which contact?")    scanf ("%s",  name);while  (link_man->pnext != null) {if  (strcmp (link_man->pnext->name, name)  == 0) {printf (" Please 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-&GT;ADDR,&NBSP;ADDR); scanf ("%d",  &link_man->pnext->year); scanf ("%s",  &link_man- >pnext->phone_num); Amend_file (link_man->pnext,name,1); return head;} Elselink_man = link_man->pnext;} Return head;} Void search_linkman (Pbook head) {char name[10];p book link_man = head;if  (! Head) {printf ("No contacts, find failed!") \ n "); return null;} printf ("which contact are you looking for?")    scanf ("%s",  name);while  (link_man->pnext != null) {if  (strcmp (link_ Man->pnext->name, name) { == 0) {printf ("%s\n",  link_man->pnext->name);p rintf ("%s \ n ",  link_man->pnext->sex);p Rintf ("%s\n",  link_man->pnext->addr);p rintf ("%d\n",  link_man->pnext->year);p rintf ("%s\n",  link_man->pnext->phone_num);p rintf ("***********************************\n"); return 0;} Elselink_man = link_man->pnext;} printf ("No find! \ n ");} Void write_file (pbook position) {file *list;if  (position != null) {if  (strcmp (file_name, "==0") {printf ("Enter the file path you want to store:"), scanf ("%s",  file_name); List = fopen (file_name,   "WT"), fprintf (list,  "%-10s",  "name"), fprintf (list,  "%-6s",  "gender"), fprintf (list,  "% -10s ", " Address "), fprintf (list, "%-8s ", " Age "), fprintf (list, "%-20s ", " number "), fprintf (list,   "%s",  "\ n");} Elselist = fopen (file_name,  "A +"), fprintf (list, "%-10s", Position->name); fprintf (list,  "% -6s ",  position->sex), fprintf (list, "%-10s ",  position->addr), fprintf (list, "%-8d ",  position->year); fprintf (list,  "%-20s ",  position->phone_num); fprintf (list, "%s ", " \ n ");} Else{list = fopen (file_name,  "w"); fprintf (list,  "%s",  "    contacts that have been destroyed    "); strcpy (file_name," n ");} Fclose (list);} Void read_file (Pbook head) {if  (head->pnext == null) {printf ("empty address Book \ n");} Else{file *list = fopen (file_name,  "R");char 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;} Elsebreak;} Fclose (list), List = fopen (file_name,  "r+"), Fseek (List, num1, seek_set);if  (i  == 0) {fprintf (list,  "%54s", " ") fprintf (list,  "%s",  "\ n");} Else{fprintf (list,  "%-10s",  position->name); fprintf (list,  "%-6s",  position->sex); fprintf (list,  "%-10s",  position->addr); fprintf (list,  "%-8d",  position->year); fprintf (list,  "%-20s",  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;} Previous   Next   return


C language Address Book write text file (with read, delete a row, modify a row)

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.