In C language, how do I create (read) the data in a file in a linked list ?, C Language

Source: Internet
Author: User

In C language, how do I create (read) the data in a file in a linked list ?, C Language

Struct defined:

Struct student {char ID [11]; // student ID char name [20]; // student name struct student * next; // next pointer pointing to struct student type variable} stu;

 

Create a file:

Void Create_File_List () {FILE * fp; if (fp = fopen ("student.txt", "rb") = NULL) /* if this file does not exist */{if (fp = fopen ("student.txt", "wb +") = NULL) {outtextxy (220,200, "the file cannot be created! ");}}}


 

Write File Information:

/*************** Function: enter the attendance student/***************/void add_student () {FILE * fp; fp = fopen ("student.txt ", "a +"); strcpy (stu. ID, ""); // strcpy (stu. name, ""); fwrite (& stu, sizeof (struct student), 1, fp); InputBox (stu. ID, 11, "Enter student ID"); outtextxy (380,200, stu. ID); Sleep (500); InputBox (stu. name, 20, "Enter the Student name"); outtextxy (380,250, stu. name); Sleep (500); fwrite (& stu, sizeof (struct student), 1, fp); fclose (fp );}

It is worth noting that when writing a file, the first data must be written in advance at the beginning. The Empty data written here is related to the NULL data at the head of the linked list.

Read the linked list from the written file:

/*************** Function: create a linked list/**************/struct student * CreateList () {struct student * pointer, * head, * q; // The head pointer is the head node of the linked list. It is the only basis for finding the linked list. If the head pointer is lost, the entire linked list cannot be found. The p pointer always points to the newly applied node; q pointer always points to the End Node struct student temp; // defines the struct alias FILE * fp; pointer = (struct student *) malloc (sizeof (struct student )); // p points to the newly opened node memory head = pointer; // There is no student score in the memory header of the newly opened header node q = pointer; // opening the memory q pointer of the End Node always points to the End Node q-> next = NULL; // It indicates the end node of the linked list The feature is that the value of the next member is NULL, which is the last node and serves as the end mark of the linked list, NULL is a symbolic constant indicating the address fp = fopen ("student.txt", "rb") with a value of 0; while (fread (& temp, sizeof (struct student), 1, fp )! = 0) // read the struct block from the file {pointer = (struct student *) malloc (sizeof (struct student )); // p points to the newly opened node memory strcpy (pointer-> ID, temp. ID); strcpy (pointer-> name, temp. name); q-> next = pointer; // after the new node is mounted to the original tail node, q = q-> next; // q pointer pointing to new tail node} q-> next = NULL; // mark the end of the linked list fclose (fp); return head ;}

 

Output data from the linked list to the screen:

/*************** Function: Output linked list
Return: pointer to the table header of the linked list/**************/void Print_List (struct student * head) {struct student * pointer; pointer = head-> next; // skip the header node without data while (pointer! = NULL) {outtextxy (x, y, pointer-> ID); outtextxy (x, y, pointer-> name); pointer = pointer-> next; // point to the next node}
}

 

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.