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

时间:2016-03-18 18:09:25

标签: c linked-list malloc global-variables

我是C编程新手。我创建了一个学生数据库,用于将学生详细信息输入到链接列表中(以结构形式" phbook")并将链接列表保存到文本文件中。我无法工作的部分是将文本文件记录读入链表。当我关闭程序然后选择"从外部文件加载"时程序运行但不更新链表。请有人帮我解决问题所在。我知道一切正常,直到我编写" readFile"和" insertFull"功能。朋友告诉我全局变量" 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;


}

1 个答案:

答案 0 :(得分:0)

在程序中

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

您正在更改列表的本地副本,这对全球电话簿列表没有影响。

您可以从参数列表中删除列表,从而更改全局变量:

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

这不是严格的编程,但它与你的其余代码一致。