My program needs to read from a binary file to a linked list, this function doing it well and allocating the right memory for it but for some reason it doing another loop before it breaks. tried looking for a good solution for it with no luck, and the last struct in the linked list getting junk.
The struct:
typedef struct
{
char id[10];
char * first_name;
char * last_name;
int age;
char gender;
char * username;
char * password;
char * description;
int hobbies[4];
struct Person * next_client;
}Person;
here is some code:
Person * input_from_file(Person * member)
{
int str_size;
Person * clients_start = NULL;
FILE * filePointerRead;
filePointerRead = fopen("input.bin", "rb");
if (filePointerRead != NULL){
while (1){
member = NULL;
member = (Person*)malloc(sizeof(Person));
fread(&member->id, sizeof(char), ID_DIGITS + 1, filePointerRead);
fread(&str_size, sizeof(int), 1, filePointerRead);
member->first_name = (char*)malloc(str_size*sizeof(char));
fread(member->first_name, sizeof(char), str_size, filePointerRead);
//more reading from file
member->next_client = NULL;
clients_start = receive_clients_info(clients_start, member); //function to put the received struct from file to end of the linked list
if (feof(filePointerRead))
break;
}
fclose(filePointerRead);
}
return clients_start;
}