c - Qsort 按字母顺序排列的字符串数组

标签 c arrays qsort

我正在尝试使用 qsort 函数对我从文件中读取的字符串数组按字母顺序进行排序。这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<io.h>

#define MAXCHAR 256


int main(int argc, char **argv){
    char tempList[MAXCHAR][MAXCHAR];
    char reader[MAXCHAR];
    FILE* fp;
    int i;
    char *n;
    int index = 0;
    if(argc != 2){
        printf("too few arguments\n");
        exit(-1);
    }

    fp=fopen(argv[1], "r");
    if(fp == NULL){
        printf("failed to open file\n");
        exit(-1);
    }
    while(!feof(fp)){
        fgets(reader, MAXCHAR, fp);
        n = strchr(reader, '\n');
        if(n != NULL){
            *n = '\0';
        }
        strcpy(tempList[index], reader);
        index++;
    }
    index--;
    for(i=0; i<index; i++){
        printf("%s\n", tempList[i]);

    }
    qsort(tempList, index, sizeof(char *), strcmp);

    for(i=0; i<index; i++){
        printf("%s\n", tempList[i]);
    }
}

当我运行程序时,列表根本没有排序。我还尝试了此网站上发布的提出类似问题的方法,它们都给我段错误。代码有问题吗?

这里是txt文件的部分内容。这是一个包含 40 个名字的列表:

Liam
Alexander
Mia
Noah
Emma
William
Charlotte
Charlotte
Mason
William
Ethan
Ethan
Liam
Alexander
Liam
Sophia
Emily
Mason
Alexander

最佳答案

你有一个真正的数组数组;不是 char* 数组。数组不是指针。 qsort 期望被排序序列中元素的步幅。由于您的序列声明为:

char tempList[MAXCHAR][MAXCHAR];

适当的元素大小是次等元素大小的大小。在这种情况下,您有一个大小为 MAXCHAR 的数组,大小为 char 的数组,大小为 MAXCHAR(数组的数组)。

因此这是错误的:

qsort(tempList, index, sizeof(char *), strcmp);
// wrong size ==============^^^^

每个元素的大小应该是:

qsort(tempList, index, sizeof tempList[0], strcmp);
// correct size ==============^^^^

你程序中的其他问题最终会让你伤心,并在你的问题下面的一般评论中涵盖,但这是阻止你的排序正常工作的根本问题。您的程序的重组版本如下所示,解决了大部分问题:

更新源

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXCHAR 256

/* properly declared for compatibility with qsort */
static int cmp_str(const void *lhs, const void *rhs)
{
    return strcmp(lhs, rhs);
}

/* main entrypoint */
int main(int argc, char **argv)
{
    char tempList[MAXCHAR][MAXCHAR];
    FILE* fp;
    size_t i, index = 0;

    if(argc != 2)
    {
        printf("too few arguments\n");
        return EXIT_FAILURE;
    }

    fp=fopen(argv[1], "r");
    if(fp == NULL)
    {
        perror(argv[1]);
        return EXIT_FAILURE;
    }

    while(index < MAXCHAR && fgets(tempList[index], sizeof(*tempList), fp) != NULL)
    {
        char *n = strchr(tempList[index], '\n');
        if(n != NULL)
            *n = 0;
        if (*(tempList[index])) /* don't insert blank lines */
            ++index;
    }

    for(i=0; i<index; i++)
        printf("%s\n", tempList[i]);
    fputc('\n', stdout);


    qsort(tempList, index, sizeof tempList[0], cmp_str);

    for(i=0; i<index; i++)
        printf("%s\n", tempList[i]);

    return EXIT_SUCCESS;
}

未经测试,但应该非常接近。

祝你好运。

关于c - Qsort 按字母顺序排列的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31751782/

相关文章:

c++ - 运行 OFX 插件

c - 如何正确创建链表

PHP MYSQL 多维数组提取值

c - qsort 给出 [错误] : invalid conversion from `int (*)(cricketer*, cricketer*)' to `int (*)(const void*, const void*)'

c - C 中的 socket() 函数错误

c - 如何使用 c 中的 write() 函数将整数 var 写入文件

c - 全局数组或 "array"的 '#define' 的替代品?

javascript - 为什么我的 forEach 循环没有编辑我的数组?

c - qsort 函数指针的类型转换

c - 在c中,如果我有 char *const* 指针,我如何访问每个元素?