链接列表,坚持文件管理

时间:2015-02-10 15:10:41

标签: c linked-list file-handling

我正在尝试从名为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;
            }

1 个答案:

答案 0 :(得分:1)

  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时以及何时不打印时要小心。