title: How to add char from a stream into a node?
body: <p>Here is my code. I am stuck on add string of stream file into my linked list. For example, here I have a file called foo. In foo, it is formatted as following</p>
  
  <p>12345678 12345678</p>
  
  <p>1233 1389732</p>
  
  <p>and so on. Which means I get each line of the file and only read the first string and add it into list. I checked add "a/b/c/d" on line 95.It works. So the insert function is working. The problem occurs on line 101. I don't know why the second line's value covered the first line's value.</p>
  
  <p>Which means, when I print the list step by step, it prints out</p>
  
  <p>a/b/c/d/12345678/</p>
  
  <p>a/b/c/d/1233/1233/</p>
  
  <p>I am stuck on why it did nit print a/b/c/d/12345678/1233 for second line? </p>
  
  <p>Any suggestions about this?
  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt; 
  #include &lt;stdlib.h&gt;
  
  
  typedef struct n{
    char *value;
    struct n *next;
  } Node;
  
  
  void printList(Node *head){
    Node *cur = head;
    while(cur!=NULL){
      printf("%s/", cur-&gt;value);
      cur = cur-&gt;next;
    }
    printf("\n");
  }
  
  void insertIntoList(Node **head, char *data){
    Node *newNode = malloc(sizeof(Node));
    if (newNode == NULL){
      perror("Failed to allocate a new node for the linked list");
      exit(1);
    }
    newNode-&gt;value = data;
    newNode-&gt;next = NULL;
  
    Node *currentList = *head;
    if(*head == NULL){    //if the linked list head is null, then add the target into linked list
      *head = newNode;
    }
    else{
      while(currentList-&gt;next!=NULL){
        currentList = currentList-&gt;next;
      }
      currentList-&gt;next = newNode;
    }
  }
  
  
  int main(int argc, char**argv){
    FILE *fileStream;
  
  
    size_t len = 0;
    char *line = NULL;
    Node *head = NULL;
  
  
    int j;
    for(j=1; j&lt;argc-2;j++){
      fileStream = fopen(argv[j], "r");
      if(fileStream == NULL){
        fprintf(stderr, "could not open");
        continue;
      }
      insertIntoList(&amp;head,"a"); /////////////Line 95
      insertIntoList(&amp;head,"b");
      insertIntoList(&amp;head,"c"); 
      insertIntoList(&amp;head,"d");
      printf("here is a try\n");
      printList(head);
      while(getline(&amp;line, &amp;len, fileStream)!=EOF){ /////////////Line 101
            char *targetNum = strtok(line, " ");
            if(strcmp(targetNum, "\n")!=0&amp;&amp;strcmp(targetNum,"\t")!=0&amp;&amp;strcmp(targetNum," ")!=0){
              printf("*****%s\n", targetNum);
              insertIntoList(&amp;head, targetNum);
              printf("######print head here is##########\n");
            printList(head);
            printf("######print head here is##########-&gt;\n");
            }
        }
        //printList(head);
    }
    return 0;
  }
  </code></pre>