Advertisement
Guest User

whowantstobe

a guest
Nov 10th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint-gcc.h>
  5.  
  6. #define MAXQ 5200
  7.  
  8. /*------------------------------------*\
  9.   #getline is valid, but not included
  10.   #in my version, tried to find an
  11.   #alternative
  12. \*------------------------------------*/
  13.  
  14. size_t getline(char **lineptr, size_t *n, FILE *stream) {
  15.     char *bufptr = NULL;
  16.     char *p = bufptr;
  17.     size_t size;
  18.     int c;
  19.  
  20.     if (lineptr == NULL) {
  21.         return -1;
  22.     }
  23.     if (stream == NULL) {
  24.         return -1;
  25.     }
  26.     if (n == NULL) {
  27.         return -1;
  28.     }
  29.     bufptr = *lineptr;
  30.     size = *n;
  31.  
  32.     c = fgetc(stream);
  33.     if (/*c == EOF*/ feof(stream)) {
  34.         return -1;
  35.     }
  36.     if (bufptr == NULL) {
  37.         bufptr = malloc(128);
  38.         if (bufptr == NULL) {
  39.             return -1;
  40.         }
  41.         size = 128;
  42.     }
  43.     p = bufptr;
  44.     while(c != EOF) {
  45.         if ((p - bufptr) > (size - 1)) {
  46.             size = size + 128;
  47.             bufptr = realloc(bufptr, size);
  48.             if (bufptr == NULL) {
  49.                 return -1;
  50.             }
  51.         }
  52.         *p++ = c;
  53.         if (c == '\n') {
  54.             break;
  55.         }
  56.         c = fgetc(stream);
  57.     }
  58.  
  59.     *p++ = '\0';
  60.     *lineptr = bufptr;
  61.     *n = size;
  62.  
  63.     return p - bufptr - 1;
  64. }
  65.  
  66. enum right_answer {
  67.     A,
  68.     B,
  69.     C,
  70.     D
  71. };
  72.  
  73. typedef struct Question {
  74.     int nth;
  75.     char *question_to;
  76.     char *answer_a;
  77.     char *answer_b;
  78.     char *answer_c;
  79.     char *answer_d;
  80.     char *answer_r;
  81.     char *cat;
  82.     //enum right_answer solution;
  83. } Question;
  84.  
  85. int main()
  86. {
  87.     char filename[] = "loim.csv";
  88.     char *line = NULL;            /* pointer to use with getline ()   */
  89.     ssize_t read = 0;             /* characters read by getline ()    */
  90.     size_t n = 0;                 /* number of bytes to allocate      */
  91.     Question **questions = NULL;  /* ptr to array of stuct question    */
  92.     char *sp = NULL;              /* start pointer for parsing line   */
  93.     char *p = NULL;               /* end pointer to use parsing line  */
  94.     int field = 0;                /* counter for field in line        */
  95.     int cnt = 0;                  /* counter for number allocated     */
  96.     int it = 0;                   /* simple iterator variable         */
  97.  
  98.     FILE *kerdes = fopen(filename, "r");
  99.     if (!kerdes) {
  100.         fprintf(stderr, "failed to open\n");
  101.         return 1;
  102.     }
  103.  
  104.     questions = (Question*) malloc(MAXQ * sizeof(Question));
  105.  
  106.     while ((read = getline(&line, &n, kerdes)) != -1) {
  107.         sp = p = line;
  108.         field = 0;
  109.         //printf("%s\n", sp);
  110.         questions[cnt] = (Question*) malloc(sizeof(Question));
  111.  
  112.  
  113.         // Cuts
  114.         while (*p != '\0') {
  115.             if (*p == ',') {
  116.                 *p = 0;
  117.  
  118.                 if (field == 0) questions[cnt]->nth = atoi(sp);
  119.                 if (field == 1) questions[cnt]->question_to = strdup(sp);
  120.                 if (field == 2) questions[cnt]->answer_a = strdup(sp);
  121.                 if (field == 3) questions[cnt]->answer_b = strdup(sp);
  122.                 if (field == 4) questions[cnt]->answer_c = strdup(sp);
  123.                 if (field == 5) questions[cnt]->answer_d = strdup(sp);
  124.                 if (field == 6) questions[cnt]->answer_r = strdup(sp);
  125.                 if (field == 7) questions[cnt]->cat = strdup(sp);
  126.  
  127.                     /*{
  128.                     if (*sp == 'A') {
  129.                         questions[cnt]->solution = 0;
  130.                     } else if (*sp == 'B') {
  131.                         questions[cnt]->solution = 1;
  132.                     } else if (*sp == 'C') {
  133.                         questions[cnt]->solution = 2;
  134.                     } else if (*sp == 'D') {
  135.                         questions[cnt]->solution = 3;
  136.                     }
  137.                 }*/
  138.  
  139.                 *p = ',';
  140.                 sp = p + 1;
  141.                 field++;
  142.             }
  143.             p++;
  144.         }
  145.         cnt++;
  146.     }
  147.  
  148.     fclose(kerdes);
  149.  
  150.     if(line) free(line);
  151.  
  152.     printf("\nQ\'s: \n\n");
  153.  
  154.     while (questions[it]) {
  155.         printf("%d %s\nA: %s\nB: %s\nC: %s\nD: %s\n\n", questions[it]->nth, questions[it]->question_to, questions[it]->answer_a, questions[it]->answer_b, questions[it]->answer_c, questions[it]->answer_d);
  156.         it++;
  157.     }
  158.     printf("\n");
  159.  
  160.  
  161.     /*
  162.     //srand(time(0));
  163.     //it = rand()%MAXQ;
  164.     //
  165.     //printf("%d %s\nA: %s\nB: %s\nC: %s\nD: %s\n\n", questions[it]->nth, questions[it]->question_to, questions[it]->answer_a, questions[it]->answer_b, questions[it]->answer_c, questions[it]->answer_d);
  166.     */
  167.  
  168.     it = 0;
  169.     while (questions[it]) {
  170.         if (questions[it]->question_to) {
  171.             free(questions[it]->question_to);
  172.         }
  173.         free(questions[it]);
  174.         it++;
  175.     }
  176.     if (questions) {
  177.         free(questions);
  178.     }
  179.  
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement