In fact, using linked lists and files to write student management system content can be divided into two major modules:
Module One: A complete list of Create, traverse, add, delete, modify;
Module two: read/write data to a file.
Module One
A linked list, mainly the correct use of malloc.
Define a struct with a struct pointer, and use malloc () to dynamically open up a struct-sized memory to be used as a node, to store multiple parallel data, and to open a memory for each dynamic to point to the next open memory with the structure pointer in the memory (if you stop dynamically opening up memory, You need to have this pointer point to NULL, as programmer knows the potential danger of pointers). The number of nodes needs to be controlled by loops. The malloc prototype is a void* type, but here it needs to be cast to struct type use. and dynamic memory does not succeed every time, Mallco () will return NULL, so we need to standardize the use of malloc ()
{3 ... Other variables 4 struct stu* pnext; }stu; 6 stu* pnew 7 if (NULL = = (Pnew = (stu*) malloc (sizeof{ten perror ("error ..."); exit (1}13 pnew-> Pnext = null;14 .../* other code */15 free(pnew), pnew = null;/* Please add this sentence */
Another thing that cannot be directly seen is that malloc allocates memory and does not initialize the resulting memory, so the value will be random in a new piece of memory.
A flexible use of pointers is required for adding, deleting, and modifying.
If the head is added/removed, pay attention to the head movement;
In the middle Add/remove, then pay attention to the use of circular positioning accurate;
To add/remove at the end, note that the Pnext (struct pointer) in the new/trailing struct is pointing to null.
Module two
The main thing is how to write the linked list into the specified file and how to read the contents of the file into the list.
Can directly write a module function to write to the file in fprintf format
1 void{3 stu* ptemp = pstu->pnext; Pstu for the head pointer ptemp for move pointer 4 5 if (fp = fopen ("Student information management. TXT", "w")) = = NULL) {7 printf ("Cannot open This file!\n " _getch (); 9 return {fprintf (file pointer," output format ", additional parameter list); ptemp = ptemp-> Fclose (FP); printf ("Out save!"); Successful write exit (0);
However, to read from a file and form a linked list, you need to first create the linked list and then read the data from the file.
1 void{3 pstu = (stu*) malloc (sizeof (STU));//Dynamically open memory, create linked list 4 pstu->pnext = NULL; 5 6 stu* Ptemp = pstu; 7 stu* pnew = NULL; 8 9 if (fp = fopen ("Student information. txt", "r") = = = {One-by-one printf ("Cannot O Pen this file\n! " _getch (); exit (0 {pnew = (stu*) malloc (sizeof(STU)), Pnew->pnext = null;20 if (fscanf (file pointer, "Format string character", 21 input list) = = {pnew);//Release unnecessary memory }26 Ptemp->pnext = pnew;28 ptemp = ptemp -
Fread and fwrite are not used here.
Fread, fwrite is a binary file to read and write in binary form, in bytes to calculate the length, according to the specified length and number of times to read the data, encountered end or completion of the specified length after reading stop. For writing data to a file, all characters can be written without effect, such as meeting a newline character and writing a newline character directly does not carry out line breaks.
The fscanf and fprintf are read from the disk file, can be binary, can be other forms, and format read and write. For example, when a newline character is encountered, a newline read or write is performed and the newline character is not written.
Here with fscanf, fprintf is mainly used for line-wrapping, format read-write.
Linked list + files A beginner's experience in implementing student management system