简体   繁体   English

C:将结构读入链接列表(从文件)

[英]C: Read Struct into Link List (from File)

I am new to C programming.我是 C 编程的新手。 I have created a student database to enter student details into a linked list (in the form of structure "phbook") and save linked list onto text file.我创建了一个学生数据库,将学生详细信息输入到链接列表(以结构“phbook”的形式)并将链接列表保存到文本文件中。 The part I cannot get to work is to read the text file records onto the linked list.我无法开始工作的部分是将文本文件记录读取到链表上。 The program runs but does not update the linked list when I close the program then select to "LOAD FROM EXTERNAL FILE".当我关闭程序然后选择“从外部文件加载”时,程序运行但不更新链接列表。 Please can someone help me figure what the problem is.请有人帮我弄清楚问题是什么。 I know everything worked until I coded the "readFile" and "insertFull" function.我知道一切正常,直到我编写了“readFile”和“insertFull”函数。 Friends have told me that the global variable "struct phbook *list = NULL;is causing the problem. Please let me know.朋友告诉我是全局变量“struct phbook *list = NULL;导致了问题。请告诉我。”

#include <stdio.h>
#include<string.h>
#include<stdlib.h>

struct phbook{
    int number;
    char name[20];
    int mark;
    struct part *next;
};

struct phbook *list = NULL;
struct phbook *find_student(int number);
void insertFull(struct phbook* list, int number, char Name[10],int mark);
int main(void)
{
    int code;
    int opt1;
    int courses, i, k, j, counter;
    for (;;){
        printf("Enter operation code: \n");
        printf("(1) ADD NEW STUDENT DETAILS: \n");
        printf("(2) SEARCH STUDENT DETAILS: \n");
        printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
        printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
        printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
        scanf(" %d", &code);
     switch (code){
         case 1 : insert();
                        break;
         case 2 : search();
                        break;
            break;
         case 3 : print();
            break;
         case 4 :
             saveToFile();
                        break;
                   case 5 :
             readFile();
                        break;
         default: printf("Illegal code\n");
}
 printf("\n");
}
    }


struct phbook *find_student(int number)
{
    struct phbook *p;
    for (p = list; p != NULL && number != p->number; p = p->next);//was sorted
    if (p != NULL && number == p->number)
        return p;
        return NULL;
}

void insert(void)
{
    struct phbook *cur;
    struct phbook *prev;
    struct phbook *new_node;
    new_node = (struct phbook*) malloc(sizeof(struct phbook));
    if (new_node == NULL){
        printf("db full er1.\n");
        return;
    }
    printf("enter student id");
    scanf("%d", &new_node->number);
    for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
    printf("Enter name: ");
    scanf("%s", &new_node->name);//readline(new_node->name, NAME_LEN)
    printf("Enter MARK: ");
    scanf("%d", &new_node->mark);

    new_node->next = cur;
    if (prev == NULL)
        list = new_node;
    else
        prev->next = new_node;
}

void search(void)
{
    int number;
    struct phbook *p;
    printf("Enter ID: ");
    scanf("%d", &number);
    p = find_student(number);
    if (p != NULL){
        printf("Name: %s\n", p->name);
        printf("Marks: %d\n", p->mark);
    }
    else
    printf("student not found.\n");
}

void print(void)
{
    struct phbook *p;
    printf("Student_Number Student_Name Student_Mark\n");
    for (p = list; p != NULL; p = p->next)
        printf("%7d %-25s %d\n", p->number, p->name, p->mark);
}
void saveToFile()
{
    FILE* fp;
    fp = fopen ("results.txt","w");


   struct phbook* curr = list;//he
   while(curr!=NULL)
   {
    fprintf(fp,"%s\n", list->name);
    fprintf(fp,"%d\n",curr->number);
    fprintf(fp, "%d\n", curr->mark);
       curr = curr->next;
   }
   fclose(fp);
}

void readFile()
{
    FILE* fp;
    if (!(fp = fopen ("results.txt", "r")))
        printf("File NOT Found");
    else{
        struct phbook *curr;
        struct phbook *prev;
        char TempName[10];
        int TempNumber;
        int TempMark;
        int done = 0;
        int count = 0;
        int success;//dummy
        curr = list;//sets it to the head (first nde of ll)
        if(list == NULL);
        printf("List is null\n");

        if(curr == NULL)
            printf("List is null\n");

        while (curr!=NULL)
        {
            curr = curr->next;
        }
        while (done == 0)
        {
            success = fscanf(fp, "%s", TempName);
            if (success == 1)
                {
                    success = fscanf(fp, "%d", &TempNumber);
                    if (success == 1)
                    {
                        success = fscanf(fp, "%d", &TempMark);

                        if(success == 1)
                        {
                            insertFull(list,TempNumber,TempName,TempMark);
                        }
                    }

                }
                else
                {
                    done = 1;
                }

        }
    }
}
void insertFull(struct phbook* list, int number, char Name[10],int mark)
{
   struct phbook *cur;
    struct phbook *prev;
    struct phbook *new_node;
    new_node = (struct phbook*) malloc(sizeof(struct phbook));
    if (new_node == NULL){
        printf("db full er1.\n");
        return;
    }

    for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
    new_node->number = number;
    new_node->mark = mark;
    strcpy(new_node->name,Name);
    new_node->next = cur;
    if (prev == NULL)
        list = new_node;
    else
        prev->next = new_node;


}

In the procedure在程序中

void insertFull(struct phbook* list, int number, char Name[10],int mark)

you are changing your local copy of list, this has no effect on the global phone book list.您正在更改列表的本地副本,这对全局电话簿列表没有影响。

You can remove list from the argument lists, thus changing the global variable:您可以从参数列表中删除列表,从而更改全局变量:

   void insertFull(int number, char Name[10],int mark)

this is not strictly good programming, but it is in line with the rest of your code.这不是严格意义上的良好编程,但它与您的其余代码一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM