简体   繁体   English

C qsort不对结构数组进行排序

[英]C qsort doesn't sort structure arrays

I have a program that's designed to read in words and split them apart, separating each and counting them up individually (words with punctuation differences are counted as different words on purpose). 我有一个程序,旨在读取单词并将它们分开,将每个单独分开并单独计数(带有标点差异的单词被故意计为不同的单词)。

typedef struct word
{
    char letters[100];
    int count;
} Word;

int compare (const void *a, const void *b)
{
    return strcmp((*(struct word **) a)->letters,(*(struct word **) b)->letters);
}

int main()
{
    int sizeCheck = 0;
    Word ** collection = malloc(sizeof(Word*));
    Word * array = malloc(sizeof(Word));

    FILE *fptr, *fout;
    char fileName[80];
    char fileNameWords[80];
    char wordLine[100];
    strcpy(fileName,"data");
    strcpy(fileNameWords,fileName);
    strcat(fileNameWords,"data.out.txt");

Where the action starts, assuming everything's fine with opening files (removed for shortness sake): 动作开始的地方,假设打开文件一切正常(为简洁起见而删除):

            int wordExists = 0;
            int t1 = 0;
            char tw1[100];


            fscanf(fptr,"%s",wordLine);
            strcpy(array->letters,wordLine);
            array->count = 1;
            collection[sizeCheck] = array;
            sizeCheck++;

            while (!feof(fptr))
            {
                wordExists = 0;
                fscanf(fptr,"%s",wordLine);
                for (t1 = 0; (t1 < sizeCheck) && (wordExists == 0); t1++)
                {
                    strcpy(tw1,array[t1].letters);
                    if (strcmp(tw1,wordLine) == 0)
                    {
                        array[t1].count += 1;
                        wordExists = 1;
                    }
                }
                if (!wordExists)
                {
                    collection = realloc(collection,(sizeCheck+1)*sizeof(Word*));
                    array = realloc(array,(sizeCheck+1)*sizeof(Word));
                    strcpy(array[sizeCheck].letters,wordLine);
                    array[sizeCheck].count = 1;
                    collection[sizeCheck] = array;
                    sizeCheck++;
                }
            }

            qsort(collection,sizeCheck,sizeof(Word*),compare);

            for (t1 = 0; t1 < sizeCheck; t1++)
            {
                fprintf(fout,"%s - %d\n",array[t1].letters,array[t1].count);
            }
            free(collection);
        }
    }
    fclose(fptr);
    fclose(fout);
    return 0;
}

Using a pointer-to-pointer method, it works for the most part, except when it comes to either the qsort function, or the fprintf parts near the bottom. 使用指针指针方法,它大部分都有效,除了qsort函数或底部附近的fprintf部分。 I'm a little stumped at this point. 在这一点上,我有点难过。 What am I doing wrong here that's preventing this from outputting a sorted file? 我在这里做错了什么阻止它输出一个排序文件? (Sorting by alphabetical order with words) (按字母顺序排序)

Only the collection array (an array of pointers) is being sorted. 仅对collection数组(指针数组)进行排序。 The values they point to (the elements of array ) are unchanged. 它们指向的值( array的元素)保持不变。 Since you fprintf the elements of array , you won't see any changes. 由于你fprintf array元素,你不会看到任何变化。

If you want to sort array , you can do that with qsort : 如果要对array进行排序,可以使用qsort进行排序:

qsort(array, sizeCheck, sizeof(Word), compareWord);

where compareWord is compareWord在哪里

int compareWord(const void *a, const void *b) {
    const Word *wa = a;
    const Word *wb = b;
    return strcmp(a->letters, b->letters);
}

Alternately, just print out the elements from collection instead of array : 或者,只打印出collection而不是array的元素:

fprintf(fout, "%s - %d\n", collection[t1]->letters, collection[t1]->count);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM