So I was tasked with grabbing a binary file, reading it into an array composed of structs then sorting it based on an array within the struct. The part that I'm stuck on is the sorting. I'm not sure if I'm supposed to sort it as an array (since the binary file is now an array) or sort it as a struct. Heres part of my code bellow.
typedef struct {
char FlightNumber[7];
char OriginCode [5];
char DestinationCode [5];
int Date [];
} FLIGHT;
int main(){
FLIGHT FlightData [3000];
/*opens file, freads it into the array then closes*/
/*trying to sort it based on OriginCode*/
int compare (const FLIGHT *a, const FLIGHT *b) {
FLIGHT *ia = (FLIGHT *)a;
FLIGHT *ib = (FLIGHT *)b;
return strcmp(ia->OriginCode, ib->OriginCode);}
qsort( FlightData, 3000, sizeof( FLIGHT ), compare);
/*to see if sorting worked...*/
for (i = 0; i < 100; i++){
printf ("%i) %s, %s, %s\n", i, FlightData[i].FlightNumber, FlightData[i].OriginCode, FlightData[i].DestinationCode );
}
}
Basically I'm lost on how to write the compare.