Home > Back-end >  Want to read through the list from a file, then copy the new file, but the procedure on flash back,
Want to read through the list from a file, then copy the new file, but the procedure on flash back,

Time:10-24

 # include 
# include
# include
# include
# include
Struct usersdata//build linked list node
{
char name[20];
Char password [20];
Struct usersdata * pnext;

};
Typedef struct usersdata users;//structure name change
Int list_number_count;
Users * file_read_listnode ()//read from the file to the list
{
The users * pHead=NULL;//set the head pointer to null pointer does not point to any node
PEnd users * and * pNew;//set up two Pointers, one to the next node node, another for the node of the last node
Int list_number_count=0;//is used to determine whether answer the number of nodes is only one
PEnd=pNew=(users *) malloc (sizeof (users));//for details and new reception space
The FILE * fl.
Fl=fopen (" users. TXT ", "r");//opened for read-only files
Printf (" test \ n ");
If (fl==NULL)//to determine whether a file pointer position is empty, empty, an error file does not exist
{
Printf (" can't read or write files \ n ");
The exit (2);
}
While (1)//circulation node
{
Printf (" test \ n ");
List_number_count + +;
If (list_number_count==1)//determine whether for the first node
{
PNew - & gt; Pnext=NULL;//set up the first node
Fscanf (fl, "% s % s", pNew - & gt; The name, pNew - & gt; Password);//read the data to the first node
PEnd=pNew; For the new//end node
PHead=pNew;//update the head pointer to point to the new node
}
The else//is not the first node
{
if(! Feof (fl))//whether the file to read and write complete, complete the return 1 (i.e., don't read files)
{
Fscanf (fl, "% s % s", pNew - & gt; The name, pNew - & gt; Password);//read the data from file
PEnd - & gt; Pnext=pNew;//the node pointer to the next node
PNew - & gt; Pnext=NULL;//the new node pointer to an empty
PEnd=pNew;//update the end node position
PNew=(users *) malloc (sizeof (users));//for the next new node to open a new space
}
The else
Free (pNew);//file to read and write back after the completion of head node pointer
The fclose (fl);
Return pHead;
}

}
}
Void write_Out (users * pHead)//from the list to a file
{
The users * ptemp;//define a temporary storage head pointer value pointer
The FILE * fl.
Fl=fopen (userscopy "text", "w +");
Printf (" is from the program output information... \n");
Sleep(1000);
Ptemp=pHead - & gt; Pnext;//to determine whether the original head pointer for temporary pointer no nodes (used for custom only used to access the head node, no value)
If (ptemp==NULL)
return;//no nodes return function
While (ptemp!=NULL)//move at the end of the chain table node pointer is not empty, empty don't have to file out of circulation in the
{
Fprintf (fl, "% s \ n \ t % s", ptemp - & gt; The name, ptemp - & gt; Password);//will list information written to the file
Ptemp=ptemp - & gt; Pnext;//update the pointer position
}
The fclose (fl);
return;
}
Int main ()
{
The users * pHead;
Printf (" is copy please later... \n");
PHead=file_read_listnode ();
//write_Out (pHead);
Printf (" write complete ");
Sleep(10000);
return 0;

}
  • Related