Hello all,
Trying to understand how to use qsort with arrays and functions reading from a file (bin). I think I have the general idea of how qsort works, but when it comes to reading from a .bin file I am completely lost! My intended output is to make it so so that it comes out in order of destination airport code, with the date and time formatted into a human-readable string. I do not understand how to convert the timestamp using time.h, but I am not worried about that for now.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct MyStruct_struct{
char FlightNum[7];
char OriginAirportCode[5];
char DestAirportCode[5];
unsigned timeStamp;
} MyStruct;
MyStruct Order[5000];
int compare (const void *v1, const void *v2)
{
const MyStruct *ia = (MyStruct *)v1;
const MyStruct *ib = (MyStruct *)v2;
int result=0;
if (ia->OriginAirportCode < ia->OriginAirportCode)
return -1;
else if (ia->OriginAirportCode > ia->OriginAirportCode)
return +1;
else
return 0;
}
int main(){
int i;
FILE * bin;
MyStruct myStruct;
bin = fopen("acars.bin", "rb");
qsort(Order, 5000, sizeof( MyStruct), compare);
for (i = 0; i < 300; i++){
printf("%i) %s, %s, %s\n", i, Order[i].FlightNum, Order[i].OriginAirportCode, Order[i].DestAirportCode);
}
fclose(bin);
return 0;
}