如何对我的char指针数组进行排序

时间:2017-01-28 19:38:46

标签: c arrays sorting

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
  

对于程序我有一个名为arr []的char *数组,我试图按字母顺序对它进行排序,但没有运气,所以在最后运行for循环时它什么都不做。我想用arr []保留offset [],因为我需要char代码和文件的偏移量

typedef struct country {
    char code_name[4];
    char name[45];
    int population;
    float life_expect;
}country;

country data[240];

int main(void) {

    char *swap;
    int a=0,c=0,d=0,n=0;
    int cmp = 1;
  

继承人arr []

    char *arr[239];
    size_t offset[239];
    char *ptr;
    int i = 0;
    int temp;
    char buf[512];
    char *token;
    char buf_write[10000];
    size_t nbytes_written;  
    size_t t_nbytes;

    FILE *fptr;
    fptr = fopen("AllCountries.dat", "r");
    int wptr;
    wptr = open("BinaryAllCountries.dat", O_RDWR);
    FILE *rptr;
    rptr = fopen("BinaryAllCountries.dat", "r");    

    do {
        if (fgets(buf, 512 , fptr)){
            //printf("%s\n",buf);
            token = strtok(buf,",;");
            while (token != NULL){          
            token = strtok(NULL, ",;");
                if (temp == 0){
                strcpy(data[i].code_name, token);
                nbytes_written = write(wptr, token, strlen(token));
                t_nbytes = t_nbytes + nbytes_written;
                //printf("code_name: %s\n", data[i].code_name);
                }
                if (temp == 1){
                strcpy(data[i].name, token);
                cmp = strcmp(data[i].name, "Virgin Islands");
                nbytes_written = write(wptr, token, strlen(token));
                t_nbytes = t_nbytes + nbytes_written;
                //printf("name: %s\n", data[i].name);
                } 
                if (temp == 6){ 
                data[i].population = atoi(token);
                nbytes_written = write(wptr, token, strlen(token));
                t_nbytes = t_nbytes + nbytes_written;
                //printf("population: %i\n", data[i].population);
                }
                if (temp == 7){
                data[i].life_expect = atof(token);
                nbytes_written = write(wptr, token, strlen(token));
                t_nbytes = t_nbytes + nbytes_written;
                //printf("life expectancy: %f\n", data[i].life_expect);
                }
            temp = temp + 1;
            }
        arr[i] = data[i].code_name;
        offset[i] = t_nbytes;
/*
        printf("%s\n",arr[i]);
        printf("%lu\n", offset[i]);
*/      printf("--------\n");

        i = i + 1;
        temp = 0;
        }
    }while (!feof(fptr));
  

这里我尝试对数组进行排序,但没有任何反应,数组保持不变

    for (c = 0 ; c < 10; c++){
            for (d = 0 ; d < 238; d++){
                if (arr[d] > arr[d+1]){
                swap     = arr[d];
                arr[d]   = arr[d+1];
                arr[d+1] = swap;
                }
        }
    }
    for (i = 0; i <239; i++){
        printf("%s\n",arr[i]);
    }
  

这里是arr []的一些输出,你可以看到它没有被排序

DZA 前 BEN BWA IOT 博鳌亚洲论坛 BDI CMR CPV CAF TCD COM COG COD CIV DJI 埃及 GNQ ERI ETH 瞎扯 GMB GHA 杜松子酒 GNB KEN LSO LBR LBY 千年发展目标 MWI MLI MRT

1 个答案:

答案 0 :(得分:2)

  

继承人我做了什么,这让我可以根据code_name字段

对结构进行排序
static int
compmi(const void *m1, const void *m2)
       {
           struct country *mi1 = (struct country *) m1;
           struct country *mi2 = (struct country *) m2;
           return strcmp(mi1->code_name, mi2->code_name);
       }


  typedef struct country {
        char code_name[4];
        char name[45];
        int population;
        float life_expect;
        size_t offset;
    }country;

qsort(data, sizeof(data)/sizeof(data[0]),sizeof(struct country),compmi);