title: Reading File Data Into a Linked List in C
body: <p>I am trying to create a simple phonebook program that reads data from a file and stores the content into specific nodes in the list. It works fine if I use my addEntry function with static data, such as:</p>
  
  <pre><code>addEntry("First", "Last", "555-555-5555");
  </code></pre>
  
  <p>If I try to read more than 1 entry from the file, each entry just appears to be whatever the last entry was in the file. For example, if my file contained:</p>
  
  <pre><code>First1
  Last1
  123-456-7890
  First2
  Last2
  987-654-3210
  </code></pre>
  
  <p>After storing the data in the list and printing, the output would look like:</p>
  
  <pre><code>First2
  Last2
  987-654-3210
  
  First2
  Last2
  987-654-3210
  </code></pre>
  
  <p>Rather than printing each specific name and number. This confuses me because this issue only occurs when I read data from the file, not when I manually type in the name and number in a function call. Here are the definitions for main and addEntry, thank you in advance.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  struct bookNode
  {
      char * firstName;
      char * lastName;
      char * phoneNumber;
      struct bookNode * next;
  } * head;
  
  FILE * fpointer;
  
  void addEntry(char * fn, char * ln, char * pn);
  void display();
  int numEntries();
  void writeBookData(struct bookNode * selection);
  
  int main()
  {
      head = NULL;
      addEntry("Test", "Name", "111-111-1111");
      addEntry("Test2", "Name2", "222-222-2222"); // These entries will work as intended
  
      int i;
      fpointer = fopen("addressbook.dat", "a+");
      if(fpointer == NULL)
      {
          printf("Error: addressbook.dat could not be opened.\n");
      }
  
      char first[20];
      char last[20];
      char num[20];
  
      while (!feof(fpointer))
      {
          fgets(first, 20, fpointer);
          fgets(last, 20, fpointer);
          fgets(num, 20, fpointer);
  
          //Removes newline characters from the ends of the names
          i = 0;
          while(first[i] != '\n')
          {
              i++;
          }
          first[i] = '\0';
          i = 0;
          while(last[i] != '\n')
          {
               i++;
          }
          last[i] = '\0';
  
          // Adds the entry from the strings with the file data in them
          addEntry(first, last, num);
      }
      fclose(fpointer);
  
      display(); // typical linked list display function
  
      int entryCount = numEntries();
      printf("There are %d entries in this Address Book\n", entryCount);
  
      return EXIT_SUCCESS;
  }
  
  void addEntry(char * fn, char * ln, char * pn)
  {
      struct bookNode * tempNode, * iterator;
      tempNode = (struct bookNode *)malloc(sizeof(struct bookNode));
      tempNode-&gt;firstName = fn;
      tempNode-&gt;lastName = ln;
      tempNode-&gt;phoneNumber = pn;
      iterator = head;
  
      // If the list is empty
      if (head == NULL)
      {
          head = tempNode;
          head-&gt;next = NULL;
      }
  
      // The list is not empty
      else
      {
          while(iterator-&gt;next != NULL)
          {
              iterator = iterator-&gt;next;
          }
          tempNode-&gt;next = NULL;
          iterator-&gt;next = tempNode;
      }
  }
  </code></pre>