Qsort没有正确排序struct元素

时间:2016-08-26 15:41:30

标签: c sorting struct io qsort

我正在编写一些读取二进制文件的代码,对其进行排序,然后将其写入并按字典顺序排序输出文件。除了对各种元素进行排序外,我不会抛出任何错误。

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;
}

我的所有元素都没有被排序。我确信我已经调用了qsort函数并正确初始化,我无法看到我错过的内容。

1 个答案:

答案 0 :(得分:3)

比较函数有这一行似乎是一个错字,因为b1被使用了两次:

    if ((*b1)->soda < (*b1)->soda)
          ^^            ^^

同样适用于:

    if ((*b1)->stomach < (*b1)->stomach)

    if ((*b1)->bat < (*b1)->bat)