Home > Back-end >  Why is the C language address book could not save the information in the text?
Why is the C language address book could not save the information in the text?

Time:10-18

At the same time that there is a bug, it is deleted cannot be deleted first input information, don't know how to solve the

# include
# include
# include

Typedef struct person//define a structure
{
Char name [10].//name
Char addr [30].//address
Char offphnum [15].//office phone
Char hmphnum [15].//home phone
Char mbphum [15].//mobile phone
Struct person * next;
} listnode, * listlink;//structure of the alias

Struct add_person
{
Char name [10].//name
Char addr [30].//address
Char offphnum [15].//office phone
Char hmphnum [15].//home phone
Char mbphum [15].//mobile phone
};

Listlink readfp ()//the file information read and translated in the linked list
{
The FILE * fp.//definition file pointer
Struct add_person persons;
Listnode * s;
Listlink head=NULL, end=NULL;
Fp=fopen (" people. TXT ", "rb");
If (fp==NULL)
{
Printf (" always open a file \ n ");
return head;
}
Fread (& amp; Persons, sizeof (struct add_person), 1, fp);
while(! Feof (fp))//whether the file end (the file position indicator whether arrived at the end)
{
S=(listnode *) malloc (sizeof (listnode));//in the linked list
Strcpy (s - & gt; The name, persons. Name);//to extract data and copy the
Strcpy (s - & gt; Addr, persons. Addr);
Strcpy (s - & gt; Offphnum, persons. Offphnum);
Strcpy (s - & gt; Hmphnum, persons. Hmphnum);
Strcpy (s - & gt; Mbphum, persons. Mbphum);
S - & gt; Next=NULL;
If (head==NULL)
The head end==s;
The else
{
End - & gt; Next=s;
End=s;
}
Fread (& amp; Persons, sizeof (struct add_person), 1, fp);
}
return head;
}

Void the Save (listlink head)//Save information
{
The FILE * fp.
The static struct add_person persons;
Listlink p1.
Fp=fopen (" people. TXT ", "wb");
For (p1=head; P1!=NULL; P1=p1 - & gt; Next);
{
Strcpy (persons. The name, p1 - & gt; Name);
Strcpy (persons. Addr, p1 - & gt; Addr);
Strcpy (persons hmphnum, p1 - & gt; Hmphnum);
Strcpy (persons mbphum, p1 - & gt; Mbphum);
Strcpy (persons offphnum, p1 - & gt; Offphnum);
Fwrite (& amp; Persons, sizeof (struct add_person), 1, fp);
}
fclose(fp);
}

Listlink the create ()//build directory function
{
Listlink s;
Listlink head=NULL, end=NULL;
While (1)
{
S=(listlink) malloc (sizeof (listnode));
Printf (" \ n \ n \ tcreate add_book 's file \ n ");
Printf (" \ n \ n \ \ t tname: (' # 'is the end) \ n \ t name: ");
The scanf (" % s ", s - & gt; Name);
If (STRCMP (s - & gt; The name, "#")==0)
break;
Printf (" \ n \ nAddr: \ toffphnum: \ thmphnum: \ tmbphnum: \ n ");
The scanf (" % s % s % s % s ", s - & gt; Addr, s - & gt; Offphnum, s - & gt; Hmphnum, s - & gt; Mbphum);
S - & gt; Next=NULL;
If (head==NULL)
The head end==s;
The else
{
End - & gt; Next=s;
End=s;
}
}
Return (the head);
}

Void Show (listlink head)//Show all information
{
P1 listnode *;
P1=head;
While (p1!=NULL)
{
Printf (" % s \ t ", p1 - & gt; Name);
Printf (" % s \ t ", p1 - & gt; Addr);
Printf (" % s \ t ", p1 - & gt; Offphnum);
Printf (" % s \ t ", p1 - & gt; Hmphnum);
Printf (" % s \ t \ n ", p1 - & gt; Mbphum);
P1=p1 - & gt; Next;
}
}

Void the Delete (listlink head)//define a Delete function
{
Listlink p1, p2,
Char name1 [10].
P1=p2=head;
If (p1==NULL)
{
Printf (" no record \ n ");
return;
}
Printf (" \ n \ n \ tDelete - both please input the name ");
The scanf (" % s ", name1);//input to delete name
While (STRCMP (p1 - & gt; The name, name1)!=0 & amp; & P1 - & gt; Next!=NULL)
{
P1 p2=;
P1=p1 - & gt; Next;
}
If (STRCMP (p1 - & gt; Name, name1)==0)//according to the judgment situation do the corresponding processing
{
If (p1==head)
P1=p1 - & gt; Next;
The else
The p2 - & gt; Next=p1 - & gt; Next;
Free (p1);
}
The else
Printf (" It is not exist in the addr - book! \n");//want to delete the record there is no
}

Void the Find (listlink head)//to Find specify the record information
{
Listlink p1.
Char name1 [10].
While (1)
{
P1=head;
Printf (" \ n \ n \ tFind - both please input the name: (' # 'is the end) \ n \ t name: ");
The scanf (" % s ", name1);//to find the name
If (STRCMP (name1, "#")==0) return;
While (STRCMP (name1, p1 - & gt; Name)!=0 & amp; & P1 - & gt; Next!=NULL)
P1=p1 - & gt; Next;
If (STRCMP (name1, p1 - & gt; Name)!=0)
Printf (" \ n \ n \ tIt is not exist in the addr - book!" );
The else
{
Printf (" \ tname: % s ", p1 - & gt; Name);
Printf (" \ taddr: % s ", p1 - & gt; Addr);
Printf (" \ tphnum: % s ", p1 - & gt; Offphnum);
Printf (" \ tphnum: % s ", p1 - & gt; Hmphnum);
Printf (" \ tphnum: % s ", p1 - & gt; Mbphum);
}
}
}

Void Input (listlink head)//information to the address book additional a man
{
Listlink s, end;
S=(listlink) malloc (sizeof (listnode));
Do
{
Printf (" \ n \ n \ tInput - both please input the sb 's Meg: \ n \ tname: (' #' is the end) \ n \ t name: ");
The scanf (" % s ", s - & gt; Name);
If (STRCMP (s - & gt; The name, "#")==0)
return;
End=head;
If (end==NULL) break;
While ((STRCMP (end - & gt; The name, s - & gt; Name)!=0) & amp; & End - & gt; Next!=NULL)
End=end - & gt; Next;
} while (STRCMP (end - & gt; The name, s - & gt; Name)==0);
Printf (" \ n \ nAddr: \ toffphnum: \ thmphnum: \ tmbphnum: \ n ");
The scanf (" % s % s % s % s ", s - & gt; Addr, s - & gt; Offphnum, s - & gt; Hmphnum, s - & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related