title: Add node to end of a list in C
body: <p>I'm writing a little program in C to read in a file and put what it contains in a linked list. I am able to create the list with the first element, but everytime I add an element, it overwrite what it added before.</p>
  
  <p>Here's my code so far : </p>
  
  <pre><code>&lt;pre&gt; 
  struct test_struct
  {
  char* valeur;
  char** tableau;
  struct test_struct *next;
  };
  FILE* ouvrirFichier(char* fichier);
  struct test_struct* create_list(char* ligne);
  struct test_struct* add_to_list(char* ligne);
  void print_list();
  struct test_struct *head = NULL;
  struct test_struct *curr = NULL;
  //struct test_struct *ptr = NULL;
  int main(int argc, char** argv)
  {
  //int i = 0, ret = 0;
  struct test_struct *ptr = NULL;
  FILE* fichier = NULL;
  char ligne[121] = {0};
  fichier = ouvrirFichier(argv[1]);
  while(fgets(ligne, 121, fichier))
  {
      ptr = add_to_list(ligne);
      printf("%sn", courant-&gt;valeur);
      printf("%sn", ptr -&gt; valeur);
  }
  print_list();
  return 0;
  }
  struct test_struct* create_list(char* ligne)
  {
  //printf("n creating list with headnode as [%d]n",val);
  struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct     test_struct));
  char* info[121] = {0};
  char separateurs[] = "[]";
  int j = 0;
  int k = 0;
  char* element;
  if(NULL == ptr)
  {
      printf("n Node creation failed n");
      return NULL;
  }
  for (element = strtok(ligne, separateurs); element; element = strtok(NULL, separateurs))
  {
      if (strcmp(element, " ") != 0 &amp;&amp; strcmp(element, "n") != 0)
      {
          info[j] = element;
          //printf("%sn", info[j]);
          //info[j] = element;
          j++;
      }
  }
  k = j;
  ptr-&gt; valeur = info[0];
  ptr -&gt; tableau = malloc(k);
  printf("%s234n", ptr -&gt; valeur);
  for (j = 1; j &lt; k; j++)
  {
      ptr -&gt; tableau[j - 1] = malloc(strlen(info[j]));
      ptr -&gt; tableau[j - 1] = info[j];
      //printf("%s ", tete -&gt; tableau[j - 1]);
  }
  ptr-&gt;next = NULL;
  head = curr = ptr;
  return ptr;
  }
  FILE* ouvrirFichier(char* entree)
  {
  FILE* fichier = NULL;
  fichier = fopen(entree, "r");
  if (fichier == NULL) // Le fichier n'a pu être ouvert
  {
      perror("Erreur d'ouverture du fichier d'entrée ");
      exit(1);
  }
  return fichier;
  }
  struct test_struct* add_to_list(char* ligne)
  {
  int j = 0;
  int k = 0;
  //char ligne[121] = {0};
  char* info[121] = {0};
  char* element;
  char separateurs[] = "[]";
  if(NULL == head)
  {
      return (create_list(ligne));
  }
  //if(add_to_end)
  //printf("n Adding node to end of list with value [%d]n",val);
  //else
  //printf("n Adding node to beginning of list with value [%d]n",val);
  struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
  if(NULL == ptr)
  {
      printf("n Node creation failed n");
      return NULL;
  }
  for (element = strtok(ligne, separateurs); element; element = strtok(NULL, separateurs))
  {
      if (strcmp(element, " ") != 0 &amp;&amp; strcmp(element, "n") != 0)
      {
          info[j] = element;
          //printf("%sn", info[j]);
          info[j] = element;
          j++;
      }
  }
  k = j;
  ptr-&gt; valeur = info[0];
  ptr -&gt; tableau = malloc(k);
  printf("%s123n", ptr -&gt; valeur);
  for (j = 1; j &lt; k; j++)
  {
      ptr -&gt; tableau[j - 1] = malloc(strlen(info[j]));
      ptr -&gt; tableau[j - 1] = info[j];
  }
  ptr-&gt;next = NULL;
  return ptr;
  }&lt;/pre&gt;
  </code></pre>
  
  <p>The content in the file look like this : ab [c] [d].</p>
  
  <p>Thanks</p>