链表,卡在文件管理上

Linked List, Stuck on file management

我正在尝试读取名为 data.txt 的文件。并将其存储到如下所示的 LL 中

Quanta 2
New 2
Ready 2
A 1 3 
B 3 4
C 5 6
D 7 9

我想读取该文件,一次一行,将每一行存储在 LL 中的一个节点处,例如 A 1 3,将在一个节点处。

想要的布局是这样的

Name    Value
Quanta  2
New     2
Ready   2
-------------------------
Process   NUT   AT
A         1     3
B         3     4
C         5     6
D         7     9

我写了下面的代码,它确实从文件中读取并正确显示名称和值的结果,但是我不明白我该怎么做,所以它一次读取一行并存储那一行进入特定节点。

这是我的代码当前显示的内容:

姓名 |值|

3       1
4       3
6       5
9       7
A       1
B       3
C       5
D       7
New     2
Quanta  2
Ready   2

我已经尝试安静地解决这个问题一段时间了,我已经正式遇到了精神障碍,如果您能提供任何帮助,我将不胜感激。

代码:

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

            #define NAME_LENGTH 20
            #define PROCESS_LENGTH 20



            //create quanta structure.

            typedef struct Quanta
            {
                char* name;
                int Value;

                struct Quanta *next;
            }Quanta; 


            Quanta* new_Q(char*, int);
            Quanta* insert_by_Q(Quanta*, Quanta*);
            void print_list(Quanta*);

            int main() 
            {
                FILE *in;
                char* name = (char*)malloc(sizeof(char*) * NAME_LENGTH);
                char filename[25];
                int Value = 0;
            //1. --------------Error Checking-----------------
                printf("File name please:\n");
                gets(filename);

                in = fopen(filename, "rb");
                if (in == NULL)
                {
                    printf("The input file failed to open.\n");
                    printf("Program cannot continue. Exiting. . .\n");
                    return 1; //Exit Program
                }
                //2. ------Linked List Operations------
                Quanta* head = NULL; //Create Empty Linked List
                Quanta* current = NULL;
                while(!feof(in)) //Check for file end
                {
                    //Read first data value to kickstart.
                    if(fscanf(in, "%s %d ", name,&Value) == EOF) 
                    {
                        break;
                    }

                    Quanta* hold = new_Q(name, Value);
                    head = insert_by_Q(head, hold);
                }

                //3. ------Print the new List------
                print_list(head);
                return 1; //Exit Success
            }

            Quanta* new_Q(char* name, int Value) {

                //Create new Quanta and malloc space
                Quanta* new = (Quanta*)malloc(sizeof(struct Quanta));
                new->name = (char*)malloc(sizeof(char) * NAME_LENGTH);
                //Set data
                strcpy(new->name, name);
                new->Value = Value;
                new->next = NULL;
                //Retun a pointer to the node
                return new;

            }

            //Inserts new node into an alphabetically sorted linked list.
            Quanta* insert_by_Q(Quanta* head, Quanta* new)
            {
                Quanta* current = NULL;
                current = head;
                if(current == NULL || strcmp(current->name, new->name) > 0)
                {
                    new->next = current;
                    return new;
                } else
                {
                    while(current->next != NULL && strcmp(current->next->name, new->name) < 0)
                    {
                        current = current->next;
                    }
                }
                    new->next = current->next;
                    current->next = new;
                    return head;

            }


            void print_list(Quanta* head)
            {
                Quanta* current;
                current = head;
                char p[] = "Name";
                char c[] = "Value";


                //Header
                printf("\n\n|%10s | %10s| \n", p, c);
                printf("-----------------------------------------------------------------------\n");


                while(current != NULL)
                {
                    printf("|%10s |%10d|\n", current->name, current->Value);
                    current = current->next;
                }
                printf("-----------------------------------------------------------------------\n");

                return;
            }
  typedef struct Quanta
    {
        char* name;
        int Value;
        int Value1; /* To hold the second integer from line 4 onwards */
        struct Quanta *next;
    } Quanta;

使用 fgets() 读取文件末尾,读取每一行后使用 sscanf() 获取所需数据。我在这里假设为前 3 行存储 Value1=0,对于其余行,此字段已正确更新。

char buf[100];
char name[20];
int val=0,i=0,val1=0;
while(fgets(buf, sizeof(buf),in) != NULL)
{
  if(i<=2)
  {
    if(sscanf(buf,"%s %d",&name,&val) == 2)
    {
      Quanta* hold = new_Q(name, val,0);
      head = insert_by_Q(head, hold);
    }
  }
  else
  {
    if(sscanf(buf,"%s %d %d",&name,&val,&val1) == 3)
    {
      Quanta* hold = new_Q(name, val,val1);
      head = insert_by_Q(head, hold);
    }
  }
  i++;
}

随着这个修复

Quanta* new_Q(char*, int,int);

保留第 4 行以后的额外值。

打印时请确保注意何时打印 val1 以及何时不打印。