title: Linked List, Stuck on file management
body: <p>I am trying to read from a file called data.txt. and store it into a LL which looks like this</p>
  
  <pre><code>Quanta 2
  New 2
  Ready 2
  A 1 3 
  B 3 4
  C 5 6
  D 7 9
  </code></pre>
  
  <p>I would like to read that file, one line at a time, storing each line at a node in a LL, e.g A 1 3, will be at one node.</p>
  
  <p>The desired layout is this </p>
  
  <pre><code>Name    Value
  Quanta  2
  New     2
  Ready   2
  -------------------------
  Process   NUT   AT
  A         1     3
  B         3     4
  C         5     6
  D         7     9
  </code></pre>
  
  <p>I have wrote the following code, which does read from the file and displays results for name and value correctly, however I do not understand how can I make it so it reads one line at a time and stores that one line into specific node. </p>
  
  <p>This is what my code Currently displays:</p>
  
  <h2>    Name | Value|</h2>
  
  <pre><code>3       1
  4       3
  6       5
  9       7
  A       1
  B       3
  C       5
  D       7
  New     2
  Quanta  2
  Ready   2
  </code></pre>
  
  <p>I have tried to solve this for quiet a while now, I have officially hit a mental block, I would appreciate any help you can offer. </p>
  
  <p>Code:</p>
  
  <pre><code>                #include &lt;stdlib.h&gt;
              #include &lt;stdio.h&gt;
              #include &lt;string.h&gt;
  
              #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,&amp;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-&gt;name = (char*)malloc(sizeof(char) * NAME_LENGTH);
                  //Set data
                  strcpy(new-&gt;name, name);
                  new-&gt;Value = Value;
                  new-&gt;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-&gt;name, new-&gt;name) &gt; 0)
                  {
                      new-&gt;next = current;
                      return new;
                  } else
                  {
                      while(current-&gt;next != NULL &amp;&amp; strcmp(current-&gt;next-&gt;name, new-&gt;name) &lt; 0)
                      {
                          current = current-&gt;next;
                      }
                  }
                      new-&gt;next = current-&gt;next;
                      current-&gt;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-&gt;name, current-&gt;Value);
                      current = current-&gt;next;
                  }
                  printf("-----------------------------------------------------------------------\n");
  
                  return;
              }
  </code></pre>