Home > Back-end >  Why data storage to file the code? For big solutions.
Why data storage to file the code? For big solutions.

Time:03-08

# include
# include
# include

Struct Telephone {
Char number [20].
char name[20];
char sex[20];
Char age [20].
Char speciaity [20].
Char Class [20].

Struct Telephone * next;
};

Typedef struct Telephone TEL;

TEL * head=NULL;

Void showmenu ();//the menu
Void Appenditem ();//to add student information
Void the print ();//browse all student information
Void Finditem1 ();//to find student information (by student id)
Void Finditem2 ();//to find student information (by name)
Void Removeitem ();//delete student information
Void Saveandfree ();//save to file
Void the Open ();//open the file
Void main ()
{
Char ch;
The Open ();//open the file
While (1) {
showmenu();//display menu
The scanf (" % c ", & amp; Ch);
The switch (ch) {
Case '1' :
Appenditem ();//to add student information
break;
Case '2' :
Print ();//browse all student information
break;
Case '3' :
Finditem1 ();//to find students information 1. According to the student id
break;
Case '4' :
Finditem2 ();//to find student information (2) by name
break;
Case '5' :
Removeitem ();//delete student information information
Print (); Results after deleting//output
break;
Case '0' :
Saveandfree ();//save and free memory
exit(0);//exit system
break;
Default:
Printf (" choose wrong!" );
break;
}
}
}

//the menu
Void showmenu ()
{
Printf (" \ n * * * * * * * * * * * * * * * * * welcome to use the student information management system * * * * * * * * * * * * * * * * * \ n ");
Printf (" \ n * * * * * * * * * * * * * * * * * according to the main menu prompted to select desired operation * * * * * * * * * * * * * * * * * \ n ");
Printf (" \ n \ t1 to add student information);
Printf (" \ n \ t2. Through all of the students information ");
Printf (" \ t3. According to the student id information query students \ n ");
Printf (" \ t4 by name query student information \ n ");
Printf (" \ n \ t5 delete student information);
Printf (" \ t0. Save and exit system! \n"); Printf (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf (" \ t please select: \ n ");
}

//to add student information
Void Appenditem ()
{
TEL * p1=NULL, * p2=NULL;

P1=(TEL *) malloc (sizeof (TEL));//application node
Printf (" input student id: \ n ");//add information
The scanf (" % s ", p1 - & gt; Number);
Printf (" input name: \ n ");
The scanf (" % s ", p1 - & gt; Name);
Printf (" input gender: \ n ");
Canf (" % s ", p1 - & gt; Sex);
Printf (" input age: \ n ");
The scanf (" % s ", p1 - & gt; The age);
Printf (" input professional: \ n ");
The scanf (" % s ", p1 - & gt; Speciaity);
Printf (" input class: \ n ");
The scanf (" % s ", p1 - & gt; The Class);
P1 - & gt; Next=NULL;//save the list

If (head==NULL) {
Head=(TEL *) malloc (sizeof (TEL));//the application space
The head - & gt; Next=p;
} else {
For (p2=head; The p2 - & gt; Next!=NULL; The p2=p2 - & gt; Next);//find node tail
The p2 - & gt; Next=p;
}
Printf (" this information has been added!" );
}

//browse all student information
Void the print ()
{
TEL * p=NULL;
If (head==NULL) {
Printf (" no record in the student information management system, please use this function after the input record! \n");
return;
}
Printf (" * * * * * * * * * * * * * * the student information management system * * * * * * * * * * * * * * * * * * * * * \ n ");//output information
Printf (" student id \ \ t t name gender \ professional \ \ t t t age class \ n ");
For (p=head - & gt; Next; p!=NULL; P=p - & gt; Next)
Printf (" % s \ \ t t % s % s % s \ \ t \ n \ t t % s % s ", p - & gt; Number, p - & gt; The name, p - & gt; Sex, p - & gt; Age, p - & gt; Speciaity, p - & gt; The Class);
}


//to find students information 1. According to the student id
Void Finditem1 ()
{
TEL * p;
Char findname [20].

Printf (" please enter to find student id: \ n ");
scanf("%s",findname);
Printf (" * * * * * * * * * * * * * * the student information management system * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf (" student id \ \ t t name gender \ professional \ \ t t t age class \ n ");

For (p=head - & gt; Next; p!=NULL; P=p - & gt; Next) {
If (STRCMP (p - & gt; Number, findname)==0)
Printf (" % s \ \ t t % s % s % s \ \ t \ n \ t t % s % s ", p - & gt; Number, p - & gt; The name, p - & gt; Sex, p - & gt; Age, p - & gt; Speciaity, p - & gt; The Class);
}
}

//to find student information (2) by name
Void Finditem2 ()
{
TEL * p;
char name[20];
Printf (" please enter a name to find: \ n ");
The scanf (" % s ", name);
Printf (" * * * * * * * * * * * * * * the student information management system * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf (" student id \ \ t t name gender \ professional \ \ t t t age class \ n ");
For (p=head - & gt; Next; p!=NULL; P=p - & gt; Next) {
If (STRCMP (p - & gt; The name, the name)==0)
Printf (" % s \ \ t t % s % s % s \ \ t \ n \ t t % s % s ", p - & gt; Number, p - & gt; The name, p - & gt; Sex, p - & gt; Age, p - & gt; Speciaity, p - & gt; The Class);
}
}


//delete student information
Void Removeitem ()
{
Char findname [20].//delete after first find
TEL * p=head - & gt; Next, * pr=head - & gt; Next;

Printf (" please enter to delete student id: \ n ");
The scanf (" % s ", & amp; Findname);
If (the head - & gt; Next==NULL) {
Printf (" this node is free! \n");
return;
}

While ((STRCMP (p - & gt; The name, findname)!=0) & amp; & P - & gt; Next!=NULL) {
Pr=p;
P=p - & gt; Next;
}
If (STRCMP (findname, p - & gt; Name)==0)//output delete information
{
Printf (" % s \ \ t t % s % s % s \ \ t \ n \ t t % s % s ", p - & gt; Next - & gt; Number, p - & gt; Next - & gt; The name, \
P - & gt; Next - & gt; Sex, p - & gt; Next - & gt; Age, p - & gt; Speciaity, p - & gt; The Class);
If (p==head - & gt; Next)
The head - & gt; Next=p - & gt; Next;
The else
Pr - & gt; Next=p - & gt; Next;

Free (p);
}
Printf (" this information has been deleted!" );
}

//save list information to a file and release the memory space
Void Saveandfree ()
{
TEL * p=NULL;
The FILE * fp.
Char * Book="books. TXT";
If (head==NULL) {
Printf (" \ n record is empty! \n");
return;
}
The else
P=the head - & gt; Next;

If ((fp=fopen (Book, wb + ""))==NULL) {
Printf (" \ n can't open file! \n");
return;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related