I am trying to scan people firstName lastName and number from a file into an array of structs. When I store the data to the array every element works fine except for temp[1].lastName
I don't understand why it is refusing to insert the last name into this element of the array any advice<
This is the struct
typedef struct
{
char firstName [20];
char lastName [20];
int numbers[6];
}KBLottoPlayer;
This is where I declare the variable size
int i,size;
FILE *in = fopen("KnightsBall.in","r");
fscanf(in,"%d",&size);
This is my function for storing the information from the file into the array
KBLottoPlayer* readArray(FILE* in, int size)
{
KBLottoPlayer* temp;
temp =(KBLottoPlayer*)malloc(sizeof(KBLottoPlayer));
int i;
if((in = fopen("KnightsBall.in", "r")) != NULL )
{
char buffer[100];
fgets(buffer, 5, in);
for(i=0;i<size;i++)
{
fscanf(in,"%s ", temp[i].firstName);
fscanf(in,"%s ", temp[i].lastName);
fscanf(in,"%d %d %d %d %d %d ", &temp[i].numbers[0], &temp[i].numbers[1], &temp[i].numbers[2], &temp[i].numbers[3], &temp[i].numbers[4], &temp[i].numbers[5]);
printf("%s %s %d %d %d %d %d %d\n ",temp[i].firstName, temp[i].lastName, temp[i].numbers[0], temp[i].numbers[1], temp[i].numbers[2], temp[i].numbers[3], temp[i].numbers[4], temp[i].numbers[5]);
}
}
else
{
printf("File is Not Exist.\n");
}
return temp;
}
This is the input file:
10
Llewellyn Mark
1 15 19 26 33 46
Ethan Willingham
17 19 33 34 46 47
Cazalas Jonathan
1 4 9 16 25 36
Siu Max
17 19 34 46 47 48
Balci Murat
5 10 17 19 34 47
Young Bryan
1 2 3 4 5 6
Anna Farach
1 3 5 7 9 10
Justin Mills
2 4 5 6 7 8
Tony Rose
1 3 4 5 6 7
Jess Jones
3 4 5 6 7 8
I expect the output to be the exact list except without the 10, but everything prints as normal except the lastName Willingham.
Actual Output:
Llewellyn Mark 1 15 19 26 33 46
Ethan 17 19 33 34 46 47
Cazalas Jonathan 1 4 9 16 25 36
Siu Max 17 19 34 46 47 48
Balci Murat 5 10 17 19 34 47
Young Bryan 1 2 3 4 5 6
Anna Farach 1 3 5 7 9 10
Justin Mills 2 4 5 6 7 8
Tony Rose 1 3 4 5 6 7
Jess Jones 3 4 5 6 7 8
Press any key to continue . . .