Hi, I've made some changes to my previous post, didn't realized you replied before I hit the submit button.
EDIT:
Sorry, further changes met but still back to square 1 of not getting sorted.
Here's the code of how I store my valuesCode:int compare(const void * p1, const void * p2) { const struct dataTuple *ps1 = p1; const struct dataTuple *ps2 = p2; int res; res = strcmp(ps1->server, ps2->server); if(res != 0) return 0; else return strcmp(ps1->name,ps2->name); }
Code:Tuple *data[7]; FILE *in, *out; char line[100]; char *tokenPtr; const char delit[2]=","; int counter, length, i, j=0,k; while((fgets(line,1000,in)) != NULL) { counter = 0; if((data[j] = (Tuple *)malloc(sizeof(Tuple)))) { if(data == NULL) { printf("Unable to allocate memory!"); exit(1); } tokenPtr = strtok(line,delit); while(tokenPtr != NULL) { counter++; switch(counter) { case 1: strcpy(data[j]->name,tokenPtr); length = strlen(data[j]->name); /* I'm trying to implement a fixed-length record so you can ignore this bit of code*/ if(length<12) { for(i=0;i<12;i++) { if(!isalpha(data[j]->name[i]) && data[j]->name[i] != '\'') { data[j]->name[i] = '\0'; } } } break; case 2: strcpy(data[j]->server,tokenPtr); length = strlen(data[j]->server); if(length<30) { for(i=0;i<30;i++) { if(!isalpha(data[j]->server[i])) { data[j]->server[i] = '\0'; } } } break; } tokenPtr = strtok(NULL,delit); } j++; } } printf("Unsorted: \n"); for(k=0;k<=count;k++) { printf("Data : %s\n", data[k]->server); } printf("\n"); qsort(data,count,sizeof(Tuple),compare); printf("Sorted: \n"); for(k=0;k<=count;k++) { printf("Data : %s\n", data[k]->server); free(data[k]); }



LinkBack URL
About LinkBacks



