title: Using fgets() to read file line by line in C
body: <p>So, I am working on getting my program to read a file line by line, storing each line (as a "string") into a linked list. </p>
  
  <p>The following while-loop </p>
  
  <pre><code>FILE *f;
  char string[longest];
  while(fgets (string, longest, f) != NULL) {    //Reading the file, line by line
      printf("-%s", string);    //Printing out each line
      insert(file_list, string);  //Why does it not change?
  }
  </code></pre>
  
  <p>The printf()-function works as expected, printing out each line. I put the hyphen as a test to see if it would separate between the lines. 
  However, when inserting the "string" into a linked list, only the first string is inserted, multiple times. </p>
  
  <p>For instance, let us say I have a text: </p>
  
  <pre><code>Roses are red,
  Violets are blue,
  Sugar is sweet,
  And so are you.
  </code></pre>
  
  <p>Now, when <strong>reading</strong> this file, and printing out the result, I get: </p>
  
  <pre><code>-Roses are red,
  -Violets are blue,
  -Sugar is sweet,
  -And so are you.
  </code></pre>
  
  <p>However, when <strong>printing</strong> out the linked list, instead of getting the same result, I get:</p>
  
  <pre><code>Roses are red,
  Roses are red,
  Roses are red,
  Roses are red,
  </code></pre>
  
  <p>Does anyone know why the "string" variable in the while-loop doesn't change after each iteration when inserting it into the linked list? It just inserts the first line four times. </p>
  
  <p>What am I missing?</p>
  
  <p><strong>UPDATE</strong>: My insert code is as follows:</p>
  
  <pre><code>void insert(node_lin *head, char *dataEntry) {
      node_lin * current = head;
  
      if(current-&gt;data == NULL) {
          current-&gt;data= dataEntry;
          current-&gt;next = NULL;
      }
  
      else {
          while(current-&gt;next != NULL) {
              current = current-&gt;next;
          }
  
          current-&gt;next = malloc(sizeof(node_lin));
          current-&gt;next-&gt;data = dataEntry;
          current-&gt;next-&gt;next = NULL;
      }
  }
  </code></pre>