the following has poped up while studying and I would like whether it does what it is supposed to. Let's say we have the following struct:
typedef struct a{
int x;
int y;
}a;
And we have a binary file where we can find info about multiple instances of the above struct, and we want to have an array of these structs and fill them one by one. Can we do the following?
a* aStruct= malloc(sizeof(a)*10); // aStruct[10]
a* temp;
int i = 0;
while(i < 10){
temp = aStruct+i++;
fread(&temp->x, sizeof(int), 1, inputFile);
fread(&temp->y, sizeof(int), 1, inputFile);
}
Does the above means that in the end, the array aStruct
will be filled with the contents from the file? If not, how can we do it?