Sorting an array of structs in C -


we given assignment in class, sort array of structs. after assignment handed in discussed doing sorting using pointers of arrays sort more efficient way people had done it.

i decided try , way well, i'm running issues haven't been able solve.

http://pastebin.com/cs3y39yu

#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h>  typedef struct stage2{//standard dec of struct field      char need;          double ring;          char fight[8];          int32_t uncle;          char game;          double war;          int8_t train;          uint32_t beds;          float crook;          int32_t feast;          int32_t rabbits;          int32_t chin;          int8_t ground;          char veil;          uint32_t flowers;          int8_t adjustment;          int16_t pets; } stage2;  void usage(){//usage method handle areas         fprintf(stderr,"file not found\n");//prints stderr         exit(1);//exits program }  int needs(const void *v1, const void *v2) {     const stage2 *p1 = v1;     const stage2 *p2 = v2;         printf("%c %c \n",p1->need,p2->need);     return 0; }   int main(int argc, char** argv){         if(argc != 3){//checks input files, 1           usage();//if not runs usage   }   int structsize = 60;//size of structs in bytes, sizeof() doesnt return correct val   char* filename = argv[1];  //pull input filename   file *file = fopen(filename, "r");//opens in read mode   fseek(file, 0, seek_end);  //goes end of file   long filesize = ftell(file);  //saves filesize   char *vals = malloc(filesize);  //allocates correct size array based on filesize   fseek(file, 0, seek_set);  //returns start of file   fread(vals, 1, filesize, file);  //reads file char array vals   fclose(file); //closes file   int structamount = filesize/structsize; //determines amount of structs need   stage2 mainarray[structamount]; //makes array of structs correct size     int j;//loop variables   int i;     printf("need, ring, fight, uncle, game, war, train, beds, crook, feast, rabbits, chin, ground, veil, flowers, adjustment, pets\n");//prints our struct names    for(i = 0; < structamount; ++){//initialises array vals         mainarray[i].need = *(&vals[0+(i*60)]);         mainarray[i].ring = *((double *)&vals[1+(i*60)]);         for(j = 0;j<9;j++){           mainarray[i].fight[j] = *(&vals[j+9+(i*60)]);         }         mainarray[i].uncle = *((int32_t *)&vals[17+(i*60)]);         mainarray[i].game = *(&vals[21+(i*60)]);         mainarray[i].war = *((double *)&vals[22+(i*60)]);         mainarray[i].train = *((int8_t *)&vals[30+(i*60)]);         mainarray[i].beds = *((uint32_t *)&vals[31+(i*60)]);            mainarray[i].crook = *((float *)&vals[35+(i*60)]);         mainarray[i].feast = *((int32_t *)&vals[39+(i*60)]);         mainarray[i].rabbits = *((int32_t *)&vals[43+(i*60)]);         mainarray[i].chin = *((int32_t *)&vals[47+(i*60)]);         mainarray[i].ground = *((int8_t *)&vals[51+(i*60)]);         mainarray[i].veil = *(&vals[52+(i*60)]);         mainarray[i].flowers = *((uint32_t *)&vals[53+(i*60)]);         mainarray[i].adjustment = *((int8_t *)&vals[57+(i*60)]);         mainarray[i].pets = *((int16_t *)&vals[58+(i*60)]);       }    for(i = 0; < structamount; ++){//prints         printf("%c, %f, %s, %d, %c, %f, %d, %u, %f, %d, %d, %d, %d, %c, %u, %d, %d \n",         mainarray[i].need,mainarray[i].ring,mainarray[i].fight,mainarray[i].uncle,mainarray[i].game,mainarray[i].war,mainarray[i].train,         mainarray[i].beds,mainarray[i].crook,mainarray[i].feast,mainarray[i].rabbits,mainarray[i].chin,mainarray[i].ground,mainarray[i].veil,         mainarray[i].flowers,mainarray[i].adjustment,mainarray[i].pets);//prints    }   free(vals);//frees memory allocated vals    stage2 *array = malloc(structamount * structsize);    for(i = 0; < structamount; ++){         array[i] = mainarray[i];     }   printf("before sort\n\n");     for(i = 0; < structamount; ++){           printf("%c, %f, %s, %d, %c, %f, %d, %u, %f, %d, %d, %d, %d, %c, %u, %d, %d \n",   array[i].need,array[i].ring,array[i].fight,array[i].uncle,array[i].game,array[i].war,array[i].train,   array[i].beds,array[i].crook,array[i].feast,array[i].rabbits,array[i].chin,array[i].ground,array[i].veil,   array[i].flowers,array[i].adjustment,array[i].pets);//prints    }   qsort(array, structamount,structsize,needs);     printf("after sort\n\n");     for(i = 0; < structamount; ++){           printf("%c, %f, %s, %d, %c, %f, %d, %u, %f, %d, %d, %d, %d, %c, %u, %d, %d \n",   array[i].need,array[i].ring,array[i].fight,array[i].uncle,array[i].game,array[i].war,array[i].train,   array[i].beds,array[i].crook,array[i].feast,array[i].rabbits,array[i].chin,array[i].ground,array[i].veil,   array[i].flowers,array[i].adjustment,array[i].pets);//prints    }    file *my_file = fopen(argv[2], "wb");   for(i = 0; < structamount; ++){           fwrite(&mainarray[i].need, sizeof(char), 1, my_file);           fwrite(&mainarray[i].ring, sizeof(double), 1, my_file);           fwrite(&mainarray[i].fight, sizeof(char[8]), 1, my_file);           fwrite(&mainarray[i].uncle, sizeof(int32_t), 1, my_file);           fwrite(&mainarray[i].game, sizeof(char), 1, my_file);           fwrite(&mainarray[i].war, sizeof(double), 1, my_file);           fwrite(&mainarray[i].train, sizeof(int8_t), 1, my_file);           fwrite(&mainarray[i].beds, sizeof(uint32_t), 1, my_file);           fwrite(&mainarray[i].crook, sizeof(float), 1, my_file);           fwrite(&mainarray[i].feast, sizeof(int32_t), 1, my_file);           fwrite(&mainarray[i].rabbits, sizeof(int32_t), 1, my_file);           fwrite(&mainarray[i].chin, sizeof(int32_t), 1, my_file);           fwrite(&mainarray[i].ground, sizeof(int8_t), 1, my_file);           fwrite(&mainarray[i].veil, sizeof(char), 1, my_file);           fwrite(&mainarray[i].flowers, sizeof(uint32_t), 1, my_file);           fwrite(&mainarray[i].adjustment, sizeof(int8_t), 1, my_file);           fwrite(&mainarray[i].pets, sizeof(int16_t), 1, my_file);   }    fclose(my_file);    return 0;//return statement } 

there link code have been working on. main issue gather, when using sorting comparison method needs (line 31) first execution of sort should return first field first 2 structs in array , print them (i aware isn't valid sorting method wanted make sure variables expected). that's not occurs; p1 variable print out expect p2 variable not. point onwards, every use of return junk (i think) values.

is there i'm missing or doing wrong?

your problem in line:

int structsize = 60;  //size of structs in bytes, sizeof() doesn't return correct val 

you're wrong; sizeof() return correct size. whatever you're doing bogus. need go basics of serialization. need write data correctly. should able read data array in single read operation. if have 60 bytes per record in input, have serious issues. note structure layout in memory wastes 7 bytes on systems (3 on some) between char need; , double ring; elements.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -