I'm writing some code that reads in a binary file, sorts it, then writes it out to and output file sorted in lexicographical order. I'm not throwing any errors with any of it except for the sorting of the various elements.
struct lab1_data
{
float goodbye;
char balance;
unsigned char clouds;
float badge;
double soda;
char bat;
short int parcel;
char vessel;
char spade;
long int cover;
unsigned long int hobbies;
short int voyage;
int stomach;
char sort;
char system [11];
unsigned short adjustment;
};
int compare(const void * a, const void * b)
{
struct lab1_data** a1 = (struct lab1_data**) a;
struct lab1_data** b1 = (struct lab1_data**) b;
if ((*a1)->soda > (*b1)->soda)
{
return 1;
}
if ((*b1)->soda < (*b1)->soda)
{
return -1;
}
if ((*a1)->stomach > (*b1)->stomach)
{
return -1;
}
if ((*b1)->stomach < (*b1)->stomach)
{
return 1;
}
if ((*a1)->bat > (*b1)->bat)
{
return -1;
}
if ((*b1)->bat < (*b1)->bat)
{
return 1;
}
//This keeps going for all elements, just varying descending/ascending order.
.
.
.
}
int main(int argc, char **argv)
{
int size = 8;
int count = 0;
int i;
struct lab1_data *lab1_struct;
lab1_struct = (struct lab1_data*) malloc (size * sizeof(struct lab1_data));
if (!lab1_struct)
{
fprintf(stderr, "Could not allocate memory");
exit(-2) ;
}
FILE *fh_i = fopen(argv[1], "rb");
FILE *fh_o = fopen(argv[2], "wb");
if (argc != 3)
{
fprintf(stderr, "Incorrect file names");
exit(1);
}
if (!fh_o)
{
fprintf(stderr, "Could not open file %s.", argv[2]);
exit(-3);
}
if (!fh_i)
{
fprintf(stderr, "Could not open file %s.", argv[1]);
exit(-3);
}
while (!feof(fh_i))
{
if (count == size)
{
size = size * 2;
lab1_struct = realloc(lab1_struct, sizeof(struct lab1_data) * size);
}
fread((void*)&lab1_struct[count].goodbye, sizeof(float), 1, fh_i);
fread((void*)&lab1_struct[count].balance, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].clouds, sizeof(unsigned char), 1, fh_i);
fread((void*)&lab1_struct[count].badge, sizeof(float), 1, fh_i);
fread((void*)&lab1_struct[count].soda, sizeof(double), 1, fh_i);
fread((void*)&lab1_struct[count].bat, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].parcel, sizeof(short), 1, fh_i);
fread((void*)&lab1_struct[count].vessel, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].spade, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].cover, sizeof(long), 1, fh_i);
fread((void*)&lab1_struct[count].hobbies, sizeof(unsigned long), 1, fh_i);
fread((void*)&lab1_struct[count].voyage, sizeof(short), 1, fh_i);
fread((void*)&lab1_struct[count].stomach, sizeof(int), 1, fh_i);
fread((void*)&lab1_struct[count].sort, sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].system, 11 * sizeof(char), 1, fh_i);
fread((void*)&lab1_struct[count].adjustment, sizeof(short), 1, fh_i);
count++;
}
struct lab1_data** plab1_struct = (struct lab1_data**) malloc (size * 8);
for (i = 0; i < count; i++)
{
plab1_struct[i] = &lab1_struct[i];
}
qsort(plab1_struct, count - 1, 8, compare);
for (i = 0; i < count; i++)
{
fwrite((void*) & (*plab1_struct[i]).goodbye, sizeof(float), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).balance, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).clouds, sizeof(unsigned char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).badge, sizeof(float), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).soda, sizeof(double), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).bat, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).vessel, sizeof(short), 1 , fh_o);
fwrite((void*) & (*plab1_struct[i]).parcel, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).spade, sizeof(char), 1 , fh_o);
fwrite((void*) & (*plab1_struct[i]).cover, sizeof(long), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).hobbies, sizeof(unsigned long), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).voyage, sizeof(short), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).stomach, sizeof(int), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).sort, sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).system, 11 * sizeof(char), 1, fh_o);
fwrite((void*) & (*plab1_struct[i]).adjustment, sizeof(short), 1, fh_o);
}
fclose(fh_i);
fclose(fh_o);
free(lab1_struct);
free(plab1_struct);
return 0;
}
None of my elements are getting sorted at all. I was sure that I had the qsort functions called and initialised properly, I can't see what I'm missing.