Thread: Sorting a struct array(numerical+alphabetical)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    18

    Sorting a struct array(numerical+alphabetical)

    (C programming)I have been trying for the past hours to sort a struct array with 10 elements. However I did not understand much at the sites where I looked for more information. Can anyone solve this, or at least give me a start.

    Here is an example from the textfile im using:

    82476,Watermelon Dorado Double India Pale Ale,31.90,355.00,Ale brittisk-amerikansk stil,Imperial/Dubbel IPA,Flaska,USA,Ballast Point Brewing,10.00

    And here is my code:

    Code:
    void listanr(void)
        
        {
    
    
            char *oneline, *tok;
            char envara[512];
            char delim[] = ",";
            FILE *fp;
            int i;
    
    
    
    
            struct vara
            {
                int nummer;
                char namn[100];
                float pris;
                float volym;
                char typ[100];
                char stil[100];
                char forpackning[20];
                char land[20];
                char producent[50];
                float alkoholhalt;
    
    
            } items[100];
    
    
    
    
            if ((fp = fopen("varor.csv", "r")) == NULL)
            {
                fprintf(stderr, "Filen varor.csv gick inte att öppna\n");
                exit(-1);
            }
    
    
            for (i = 0; i < 100 && fgets(envara, 512, fp); i++)
            {
                envara[strlen(envara) - 1] = '\0';
                oneline = strdup(envara);
    
    
                tok = strtok(oneline, delim);
                items[i].nummer = atoi(tok);
                tok = strtok(NULL, delim);
                strncpy(items[i].namn, tok, (max(strlen(tok), sizeof(items[0].namn))));
                tok = strtok(NULL, delim);
                items[i].pris = atof(tok);
                tok = strtok(NULL, delim);
                items[i].volym = atof(tok);
                tok = strtok(NULL, delim);
                strncpy(items[i].typ, tok, strlen(tok));
                tok = strtok(NULL, delim);
                strncpy(items[i].stil, tok, strlen(tok));
                tok = strtok(NULL, delim);
                strncpy(items[i].forpackning, tok, strlen(tok));
                tok = strtok(NULL, delim);
                strncpy(items[i].land, tok, min(strlen(tok), sizeof(items->land)));
                tok = strtok(NULL, delim);
                strncpy(items[i].producent, tok, strlen(tok));
                tok = strtok(NULL, delim);
                items[i].alkoholhalt = atof(tok);
    
    
                printf("nummer: %d\n"
                    "namn: %s\n"
                    "pris: %f\n"
                    "volym: %f\n"
                    "typ: %s\n"
                    "stil: %s\n"
                    "forpackning: %s\n"
                    "land: %s\n"
                    "producent: %s\n"
                    "alkoholhalt: %f\n\n",
                    items[i].nummer,
                    items[i].namn,
                    items[i].pris,
                    items[i].volym,
                    items[i].typ,
                    items[i].stil,
                    items[i].forpackning,
                    items[i].land,
                    items[i].producent,
                    items[i].alkoholhalt
    );
    
    
          
    
    
                free(oneline);
            }
    
    
            fclose(fp);
        }
    Last edited by emano12; 01-23-2018 at 01:17 PM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  2. Alphabetical sorting in a character array?
    By YPavluk in forum C Programming
    Replies: 10
    Last Post: 10-28-2011, 08:35 AM
  3. C++ Alphabetical sorting, etc Help
    By Lucifix in forum C Programming
    Replies: 5
    Last Post: 05-17-2010, 01:29 AM
  4. Alphabetical sorting function
    By typer in forum C Programming
    Replies: 6
    Last Post: 05-20-2006, 02:35 AM
  5. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM

Tags for this Thread