I have been trying to read data from file using fscanf with this format:
fscanf(members, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]", temp_node->name, temp_node->personal_names, &temp_node->mem_id, temp_node->email, temp_node->boat_class, temp_node->boat_name);
Read function:
member_node read_member(FILE * members){
int scan = 0;
member *temp_node;
temp_node = calloc(1,sizeof(temp_node));
scan = fscanf(members, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]", temp_node->name, temp_node->personal_names, &temp_node->mem_id, temp_node->email, temp_node->boat_class, temp_node->boat_name);
if(scan==EOF){
free(temp_node);
return NULL;
} else{
return temp_node;
}
}
Struct def:
typedef struct member{
char name[20];
char personal_names[80];
int mem_id;
char email[50];
char boat_class[30];
char boat_name[30];
}member;
When it starts to read the second line(personal_names) it reads correctly, however it wont stop and starts reading from start in to the same array.
Output in debugger:
name - Morris
personal_names - Huw Rhys MorganMorrisHuw Rhys Morgan 1015... util the array is full
mem_id - 1015
email - h.w.m.morris@\0David\n235\nd.price....
boat_class - noclub.club.uk...
boat_name - Flyer\0f.long...
Expected output:
name - Morris
personal_names - Huw Rhys Morgan
mem_id - 1015
email - h.w.m.morris@nolcub.clubs.uk
boat_class - Flyer
boat_name - The Blue One
The file has this format:
Morris
Huw Rhys Morgan
1015
h.w.m.morris@noclub.clubs.uk
Flyer
The Blue One
Price
David
235
d.price@noclub.clubs.uk
Invader
Goldcrest
Long
Fred
2
f.long@long.people.uk
AberSloop
Jaqueta