C - The linked list next node is null

I am trying to get the next value of my structure using a linked list, but the next value is null.

I know I am not doing this right, I hope you can help me solve my problem.

Input file:

new_employee Hasselhoff David PDG F-13000 1 
new_employee Anderson Pamela DDR F-31000 2 
new_employee LeeNolin Gena DDR F-94270 3
new_employee Charvet David DPR F-54000 4

      

Code:

char    *my_strcat(char *s1, char *s2)
{
  char  *tmp;
  int   i;
  int   j;

  i = -1;
  j = 0;
  tmp = malloc(sizeof(char) * (strlen(s1) + strlen(s2)) + 1);
  while (s1[++i])
    tmp[j++] = s1[i];
  i = -1;
  while (s2[++i])
    tmp[j++] = s2[i];
  tmp[j] = '\0';
  return (tmp);
}

char    *read_func()
{
  char *str;
  char line[256];

  str = malloc(sizeof(char) * 256);
  while (fgets(line, sizeof(line), stdin))
    str = my_strcat(str, line);
  return (str);
}

void            fill_struct(t_emp *s_emp)
{
  char          *read;

  read = read_func();
  if (strstr(read, "new_employee"))
    {
      while (s_emp->next != NULL)
        s_emp = s_emp->next;
      s_emp->next = malloc(sizeof(t_emp));
      strdup(strtok(read, " "));
      s_emp->name = strdup(strtok(NULL, " "));
      s_emp->forename = strdup(strtok(NULL, " "));
      s_emp->job = strdup(strtok(NULL, " "));
      s_emp->zipcode = strdup(strtok(NULL, " "));
      s_emp->id = atoi(strtok(NULL, " "));
      s_emp = s_emp->next;
    }
}

void    print_emp(t_emp *s_emp)
{
  while (s_emp != NULL)
    {
      printf("******************************\n");
      printf("%s %s\n", s_emp->forename, s_emp->name);
      printf("position: %s\n", s_emp->job);
      printf("city: %s\n", s_emp->zipcode);
      s_emp = s_emp->next;
    }
}

      

Main function:

int     main()
{
  t_emp s_emp;

  if (!isatty(STDIN_FILENO))
    {
      fill_struct(&s_emp);
      print_emp(&s_emp);
    }
  else
    show_help();
}

      

output:

******************************
David Hasselhoff
position: PDG
city: F-13000
******************************
(null) (null)
position: (null)
city: (null)

      

desired result:

******************************
David Hasselhoff
position: PDG
city: F-13000
******************************
Pamela Anderson
position: DDR
city: F-31000

      

Thank!

+3


source to share


1 answer


You need to call fill_struct

in a loop; the operator if

will only run once and it will stop after the first employee. The best way to do this is to simply change the operator if

in this function to while

. For example:

//...
while (strstr(read, "new_employee") != NULL)
//...

      

Using the fact that it strstr

will return NULL if the next occurrence of "new_employee" is not found.

As noted in the comments, your first one s_emp = s_emp->next

in the loop is while

not needed, as it will always be NULL

; you don't assign s_emp->next

until the next line.



Also, your first call is strdup

invalid, as you need the free

pointer returned from that call. So store it in a variable and free it later.

Also: why is your function my_strcat

so complex? strcat

is just the equivalent of this:

char *p = str1;

while (*p) //Traverse to the end of str1 
  p++;
while (*p++ = *str2++) //Will stop with copying '\0' over
  ;

return str1;

      

It shouldn't be any harder than this.

+2


source







All Articles