title: How to populate a linked list from a file
body: <p>I am trying to populate a linked list reading in from a file. I have been attempting to use <code>strtok()</code> to break apart a string using <code>" "</code> as a delimiter. When I call my <code>addEntry()</code> function it is only adding the first letter of each string to a new node. I realize this is probably a simple oversight, so please excuse my ignorance as I am still quite new to coding.</p>
  
  <p>The specific section of code I am having trouble with is:</p>
  
  <pre><code>void readData(struct boggleDataNode **headRef){
      //initializing variables including
      //opening the input file
      int counter = 96;
      int i = 0;
      char dataLine[LENGTH];
      char *data;
      char *fileName = "BoggleData.txt";
      FILE *filePointer;
  
      printf("Trying to open file %s\n", fileName);
      filePointer = fopen(fileName, "r"); //read mode
      printf("Successfully opened file %s\n", fileName);
  
      if (filePointer == NULL){
          perror("Error while opening file. \n");
          exit(0);
      }
  
      //loop until find end of file
      while ( fgets(dataLine, LENGTH, filePointer) != NULL){
          //I feel like i should be using a nested loop here but I can't figure out how to do it 
          printf("dataLine = %s\n", dataLine);
          data = strtok(dataLine, " ");
          printf("data = %s\n", data);
          //fgets(data, 3, filePointer);
          addEntry(headRef, data);
          //nested loop should end here
      }
  }
  </code></pre>
  
  <p>My output is only 16 nodes, the first letter from each line, when it should be 96 nodes long.
  The file which I am reading from is:</p>
  
  <pre><code>D R L X E I
  C P O H S A
  N H N L Z R
  W T O O T A
  I O S S E T
  N W E G H E
  B O O J A B
  U I E N E S
  P S A F K F
  I U N H M Qu
  Y R D V E L
  V E H W H R
  I O T M U C
  T Y E L T R
  S T I T Y D
  A G A E E N
  </code></pre>
  
  <hr>
  
  <p>In case the full code is needed:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define LENGTH 80
  
  struct boggleDataNode {
      char data[3];
      struct boggleDataNode *nextData;
  };
  
  struct boggleDieSideNode {
      char dieSideData[3];
      struct boggleDieSideNode *nextSide;
  };
  
  void readData(struct boggleDataNode **);
  void addEntry(struct boggleDataNode **, char *);
  void display(struct boggleDataNode *);
  
  int main() {
  
      int counter = 0;
  
      struct boggleDataNode * head = NULL;
      struct boggleDieSideNode *head1 = NULL;
      readData(&amp;head);
      display(head);
  
      system("pause");
      return 0;
  }
  
  void readData(struct boggleDataNode **headRef){
      //initializing variables including
      //opening the input file
      int counter = 96;
      int i = 0;
      char dataLine[LENGTH];
      char *data;
      char *fileName = "BoggleData.txt";
      FILE *filePointer;
  
      printf("Trying to open file %s\n", fileName);
      filePointer = fopen(fileName, "r"); //read mode
      printf("Successfully opened file %s\n", fileName);
  
      if (filePointer == NULL){
          perror("Error while opening file. \n");
          exit(0);
      }
  
      //loop until find end of file
      while ( fgets(dataLine, LENGTH, filePointer) != NULL){
          printf("dataLine = %s\n", dataLine);
          data = strtok(dataLine, " ");
          printf("data = %s\n", data);
          //fgets(data, 3, filePointer);
          addEntry(headRef, data);
      }
  
      fclose(filePointer);
  
  }//cose readData
  
  void addEntry(struct boggleDataNode **headRef, char *data){
  
      //allocate node
      struct boggleDataNode* temp = (struct boggleDataNode*) malloc(sizeof(struct boggleDataNode));
  
      //put in data
      strcpy(temp-&gt;data, data);
  
      // link the old list off the new node
      temp-&gt;nextData = (*headRef);
  
      // move the head to point to the new node
      (*headRef) = temp;
  }
  
  void display(struct boggleDataNode *headRef){
  
      struct boggleDataNode *helper = headRef;
  
      if (helper == NULL){
          return;
      }
      else{
          printf("============================================\n");
          printf("Node #        Data\n");
          printf("============================================\n");
  
          int counter = 1;
  
          while (helper != NULL){
              printf("%5d \t %2s\n", counter++, helper-&gt;data);
              helper = helper-&gt;nextData;
          }
      }
      printf("\n");
      system("pause");
  }
  </code></pre>