0

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 . . .

Ethan W
  • 1
  • 2
  • How many structures are you allocating with your call to `malloc`? – Some programmer dude Jan 24 '19 at 12:38
  • Also, when and where do you read the "size"? And why do you pass `in` as an argument instead of defining it locally inside the function? – Some programmer dude Jan 24 '19 at 12:39
  • Oh and please read [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Some programmer dude Jan 24 '19 at 12:42
  • Sorry just updated I have the variable size defined in my main function. I pass it as a argument rather than defining it inside a function because I am going to need to use this variable for several more part throughout the project I am working on – Ethan W Jan 24 '19 at 12:42
  • Inside the function you *reassign* the variable `in`, by *reopening* the file. That's not needed. Neither is your `fgets` call to re-read the size. Either don't pass the file and open locally inside the function; Or pass the file and don't reopen in the function. – Some programmer dude Jan 24 '19 at 12:44
  • 1
    You never check return values of `fscanf` – Gerhardh Jan 24 '19 at 12:46
  • Oh alright I see what you are saying sorry about that – Ethan W Jan 24 '19 at 12:47

1 Answers1

1

You need to allocate enough space for the number of structures, you want to read. Currently you only allocate for a single item here:

temp = (KBLottoPlayer*)malloc(sizeof(KBLottoPlayer));

but you have to allocate for size items to avoid writing out of bounds:

temp = (KBLottoPlayer*)malloc(sizeof(KBLottoPlayer) * size);

otherwise you observe undefined behaviour.

Ctx
  • 18,090
  • 24
  • 36
  • 51