tdulik

Phonebook - CSV file sorting

Nov 10th, 2020
640
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. typedef struct {
  7.     int id;
  8.     char firstname[64], surname[64], address[256], phonenr[32];
  9. } tPerson;
  10.  
  11. enum {
  12.     ID, FIRSTNAME, SURNAME, ADDRESS, PHONENUMBER
  13. } sortBy = PHONENUMBER;
  14.  
  15. int comparePersons(const void *p1, const void *p2) {
  16.     const tPerson * c1 = p1, *c2 = p2;
  17.     switch (sortBy) {
  18.     case ID:
  19.         return c1->id - c2->id;
  20.         break;
  21.     case FIRSTNAME:
  22.         return strcmp(c1->firstname, c2->firstname);
  23.         break;
  24.     case SURNAME:
  25.         return strcmp(c1->surname, c2->surname);
  26.         break;
  27.     case ADDRESS:
  28.         return strcmp(c1->address, c2->address);
  29.         break;
  30.     case PHONENUMBER:
  31.         return strcmp(c1->phonenr, c2->phonenr);
  32.         break;
  33.     }
  34.     return 0;
  35. }
  36.  
  37. int main(int argc, char *argv[]) {
  38.     tPerson array[1000];
  39.     tPerson demo_arr[] = {
  40.             { 0, "Frantisek", "Dobrota", "Blizka 23, 11100 Praha", "777666555444" },
  41.             { 1, "Anna", "Peprna", "Daleka 1, Plzen", "606055444" },
  42.             { 2, "Viliam", "Razny", "Hluboka 3", "257000111" },
  43.             { 3, "Zdenek", "Pomaly", "Krizova 5", "800333444" },
  44.             { 4, "Rene", "Drobny", "Vysehradska 3", "774888999" },
  45.             { 5, "Brona","Chalupova", "Mladcova 4", "356222333" },
  46.             { 6, "Cyril", "Metodej", "U Zahradek 123", "456321000" },
  47.         { 7, "Adam", "Doe", "Riverside 111", "956321000" }
  48.     };
  49.     if (argc>1)     //processing command line parameters here
  50.         switch (argv[1][0]) {
  51.             case 'i': sortBy = ID; break;
  52.             case 'f': sortBy = FIRSTNAME; break;
  53.             case 's': sortBy = SURNAME; break;
  54.             case 'a': sortBy = ADDRESS; break;
  55.             case 'p': sortBy = PHONENUMBER; break;
  56.             default:
  57.                 printf("1. parameter is a character specifying which column will be the sorting key\n"
  58.                         "i: ID, f: first name, s: surname, a: address, p: phone number\n");
  59.         }
  60.     char buffer[200];
  61.     int i=0;
  62.     puts("Please enter content of a CSV file, end by CTRL+Z");
  63.     while (fgets(buffer, 200, stdin)!=NULL && i<1000) {
  64.         int status=sscanf(buffer, "%d;%[^;];%[^;];%[^;];%[^;\n];",
  65.                &array[i].id, array[i].firstname, array[i].surname,
  66.                array[i].address, array[i].phonenr);
  67.         if (status==5) i++;
  68.         else {
  69.             printf("Error on line %d.: %s\n", i, buffer);
  70.         }
  71.     }
  72.     int count = i;
  73.     qsort(array, count, sizeof(tPerson), comparePersons); // sorting here
  74.  
  75.     for (int i=0; i<count; i++)  //printing the results
  76.         printf("%d; %s; %s; %s; %s\n", array[i].id, array[i].firstname, array[i].surname, array[i].address, array[i].phonenr);
  77.     exit(EXIT_SUCCESS);
  78. }
  79.  
RAW Paste Data