It looks like function load doesnt insert anything. If I want to know the size of link list in the funtion getCount, it returns me number 0, but there are 3 lines of data in the file I read from

typedef struct employees{ 
  char first_name[30];
  char second_name[30];
  long long int ID;
  float salary;
  struct zamestnanci* next;
} EMPLOYEES;

void load(FILE *f, EMPLOYEES** head){

  int i;

  if((f = fopen("zamestnanci.txt", "r")) == NULL)   
  { 
        printf("Zaznamy neboli nacitane\n");
        exit(0);
  }
  for(i = 0; i < 3; i++)    {
    EMPLOYEES* temp1 = (EMPLOYEES*) malloc (sizeof(EMPLOYEES));
    while(fscanf(f, "%s %s %lld %f", &temp1->first_name, &temp1->second_name, &(temp1->ID), &temp1->salary) == 4)
    {

        temp1->next=NULL;
        if(head == NULL){
            temp1->next= head;
            head=temp1; 
        }       

        EMPLOYEES* temp2 = head;
        while(temp2->next!= NULL)
        {
            temp2->next= temp1;
        }
    }

This is function getCount, which return count = 0, but there are 3 lines in the file:

int getCount(EMPLOYEES** head)  
{  
  int count = 0; // Initialize count  
  ZAMESTNANCI* current = head; // Initialize current  
  while (current != NULL)  
  {  
    count++;  
    current = current->dalsi;  
  }  
  printf("Count %d\n", count);
  return count;  
}  

This is main:

 int main()
 {
   FILE *f;
   struct EMPLOYEES*head;

   head = NULL;

   load(f, &head);
   getCount(head);

}

1 Answers

1
bruno On Best Solutions

In

typedef struct employees{ 
  char first_name[30];
  char second_name[30];
  long long int ID;
  float salary;
  struct zamestnanci* next;
} EMPLOYEES;

I suppose struct zamestnanci* next; is struct employees* next;

In

 for(i = 0; i < 3; i++)    {
   EMPLOYEES* temp1 = (EMPLOYEES*) malloc (sizeof(EMPLOYEES));
   while(fscanf(f, "%s %s %lld %f", &temp1->first_name, &temp1->second_name, &(temp1->ID), &temp1->salary) == 4)
   {
      ...
   }

in the scanf &temp1->first_name, &temp1->second_name, must be temp1->first_name, temp1->second_name,

all is read into the while at the first turn of the for and all the time using the same unique allocated EMPLOYEES

probably you wanted a if rather than a while and adding an else to go out of the for after you free temp1

In

   temp1->next=NULL;
   if(head == NULL){
       temp1->next= head;
       head=temp1; 
   }       

head == NULL is impossible, you wanted to check *head == NULL (note even head is not a pointer to a pointer temp1->next= head; is useless because you do again temp1->next=NULL;)

In

   EMPLOYEES* temp2 = head;
   while(temp2->next!= NULL)
   {
       temp2->next= temp1;
   }

you do not modify the value of the variable temp2, fortunately because you construct wrongly the list head->next is always NULL and you do not loop forever

Probably you wanted to add the new cell at the end of the list so you need to replace

   temp1->next=NULL;
   if(head == NULL){
       temp1->next= head;
       head=temp1; 
   }       

   EMPLOYEES* temp2 = head;
   while(temp2->next!= NULL)
   {
       temp2->next= temp1;
   }

By

  temp1->next=NULL;
  if (*head != NULL) {
    EMPLOYEES** temp2 = &(*head)->next;

    while (*temp2 != NULL)
      temp2 = &(*temp2)->next;

    *temp2 = temp1;
  }
  else
    *head = temp1;

As said in a remark int getCount(EMPLOYEES** head) must be int getCount(EMPLOYEES* head)

Also in that function current = current->dalsi; must be current = current->next; and ZAMESTNANCI* current = head; must be EMPLOYEES* current = head;

Note in

fscanf(f, "%s %s %lld %f", &temp1->first_name, &temp1->second_name, &(temp1->ID), &temp1->salary) == 4)

there is also no protection against an overflow of first_name and second_name in case at least one of them have more than 29 characters, and you can go out of the allocated block with an undefined behavior

In main

 struct EMPLOYEES*head;

must be

EMPLOYEES*head;

It is useless to have f in main and to give it in argument because it is not set in main


Doing all the changes :

#include <stdio.h>
#include <stdlib.h>

typedef struct employees{ 
  char first_name[30];
  char second_name[30];
  long long int ID;
  float salary;
  struct employees* next;
} EMPLOYEES;

void load(EMPLOYEES** head){
  FILE *f;
  int i;

  if((f = fopen("zamestnanci.txt", "r")) == NULL)   
  { 
        printf("Zaznamy neboli nacitane\n");
        exit(0);
  }
  for(i = 0; i < 3; i++)    {
    EMPLOYEES* temp1 = (EMPLOYEES*) malloc (sizeof(EMPLOYEES));
    if(fscanf(f, "%29s %29s %lld %f", temp1->first_name, temp1->second_name, &(temp1->ID), &temp1->salary) == 4)
    {
      temp1->next=NULL;
      if (*head != NULL) {
        EMPLOYEES** temp2 = &(*head)->next;

        while (*temp2 != NULL)
          temp2 = &(*temp2)->next;

        *temp2 = temp1;
      }
      else
        *head = temp1;
    }
    else {
      free(temp1);
      break;
    }
  }
}

int getCount(EMPLOYEES* head)  
{  
  int count = 0; // Initialize count  
  EMPLOYEES* current = head; // Initialize current  
  while (current != NULL)  
  {  
    printf("%s %s %lld %f\n", current->first_name, current->second_name, current->ID, current->salary);
    count++;  
    current = current->next;  
  }  
  printf("Count %d\n", count);
  return count;  
}  

int main()
 {
   EMPLOYEES * head = NULL;

   load(&head);
   getCount(head);

   return 0;
}

Compilation and execution :

[email protected]:/tmp $ gcc -g -pedantic -Wextra -Wall f.c
[email protected]:/tmp $ cat zamestnanci.txt 
js back 1 2
lv beethoven 3 4
wa mozart 5 6
[email protected]:/tmp $ ./a.out
js back 1 2.000000
lv beethoven 3 4.000000
wa mozart 5 6.000000
Count 3