c - How to sort file output? -
i have program takes user input , creates records several fields user enters. user asked enter first , last name of person, address, city, state, zipcode, , phone #. i'm trying sort records in alphabetical order in relation city of each record. how go this, i'm pretty stumped on how sort based on 1 variable , print entire record properly.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct record record; struct record { char fname[51]; char lname[51]; char address[51]; char city[51]; char state[51]; char zipcode[51]; char phonenumber[51]; record *next; }; int main() { file *filewriter; const char filename[] = "data.txt"; char answer = '\0'; record *records = null; record *records_first = null; record *records_previous = null; filewriter = fopen(filename,"wt"); if(filewriter != null) { for( ; ; ) { records = (record*) malloc(sizeof(record)); if(records_first == null) records_first = records; if(records_previous != null) records_previous->next = records; records = records_first; printf("first name: \n"); scanf("%s", records->fname); fprintf(filewriter,"%s\t",records->fname); printf("last name: \n"); scanf("%s", records->lname); fprintf(filewriter,"%s\t",records->lname); printf("address: \n"); scanf(" %[^\n]", records->address); fprintf(filewriter,"%s\t",records->address); printf("city: \n"); scanf("%s", records->city); fprintf(filewriter,"%s\t",records->city); printf("state: \n"); scanf("%s", records->state); fprintf(filewriter,"%s\t",records->state); printf("zipcode: \n"); scanf("%s", records->zipcode); fprintf(filewriter,"%s\t",records->zipcode); printf("phone number: \n"); scanf("%s", records->phonenumber); fprintf(filewriter,"%s\t\n",records->phonenumber); records->next = null; records_previous = records; printf("are there anymore records? [y/n] "); scanf(" %c", &answer); if(tolower(answer) != 'y') { free(records); fclose(filewriter); break; } } } else printf("error opening file."); return 0; } **edited version**
i tried use qsort(). ended fatal error. on top of that, how print entire record when i'm able sort 1 field?
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <ctype.h> struct record { char fname[51]; char lname[51]; char address[51]; char city[51]; char state[51]; int zipcode; int phonenumber; }; int compare_city(const void *const p1, const void *const p2) { struct record *r1 = (struct record *) p1; struct record *r2 = (struct record *) p1; return strcmp(r1->city, r2->city); } int main() { file *filewriter; const char filename[] = "data.txt"; char answer = 'y'; int size = 1; int = 0; int count = 1; struct record *records = null; struct record *records_temp = null; filewriter = fopen(filename,"wb"); if(filewriter != null) while(answer == 'y' || answer == 'y') { if(records_temp == null) { struct record *records_temp = realloc(records,(size)*sizeof(*records)); } records = records_temp; printf("first name: \n"); scanf("%s", records[i].fname); printf("last name: \n"); scanf("%s", records[i].lname); printf("address: \n"); scanf(" %[^\n]", records[i].address); printf("city: \n"); scanf("%s", records[i].city); printf("state: \n"); scanf("%s", records[i].state); printf("zipcode: \n"); scanf("%s", records[i].zipcode); printf("phone number: \n"); scanf("%s", records[i].phonenumber); //stores record info printf("are there anymore records? [y/n] "); scanf(" %c", &answer); if(tolower(answer) == 'y') { i++; count++; } for(i = 0; < count ; i++) { qsort(records, count, sizeof(struct record), compare_city); fprintf(filewriter,"%s\n",records[i].fname); fprintf(filewriter,"%s\n",records[i].lname); fprintf(filewriter,"%s\n",records[i].address); fprintf(filewriter,"%s\n",records[i].city); fprintf(filewriter,"%s\n",records[i].state); fprintf(filewriter,"%d\n",records[i].zipcode); fprintf(filewriter,"%d\n",records[i].phonenumber); } free(records); } return 0; }
as is, have list structure, cannot sorted qsort()
, can use array instead, , allocate dynamically using malloc()
+ realloc()
.
to sort entries can use qsort()
, example suppose want sort first name, then
int compare_first_name(const void *const p1, const void *const p2) { struct record *r1 = (struct record *) p1; struct record *r2 = (struct record *) p1; return strcmp(r1->fname, r2->fname); }
and then
qsort(base, count, sizeof(struct record), compare_first_name);
Comments
Post a Comment