Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint-gcc.h>
- #define MAXQ 5200
- /*------------------------------------*\
- #getline is valid, but not included
- #in my version, tried to find an
- #alternative
- \*------------------------------------*/
- size_t getline(char **lineptr, size_t *n, FILE *stream) {
- char *bufptr = NULL;
- char *p = bufptr;
- size_t size;
- int c;
- if (lineptr == NULL) {
- return -1;
- }
- if (stream == NULL) {
- return -1;
- }
- if (n == NULL) {
- return -1;
- }
- bufptr = *lineptr;
- size = *n;
- c = fgetc(stream);
- if (/*c == EOF*/ feof(stream)) {
- return -1;
- }
- if (bufptr == NULL) {
- bufptr = malloc(128);
- if (bufptr == NULL) {
- return -1;
- }
- size = 128;
- }
- p = bufptr;
- while(c != EOF) {
- if ((p - bufptr) > (size - 1)) {
- size = size + 128;
- bufptr = realloc(bufptr, size);
- if (bufptr == NULL) {
- return -1;
- }
- }
- *p++ = c;
- if (c == '\n') {
- break;
- }
- c = fgetc(stream);
- }
- *p++ = '\0';
- *lineptr = bufptr;
- *n = size;
- return p - bufptr - 1;
- }
- enum right_answer {
- A,
- B,
- C,
- D
- };
- typedef struct Question {
- int nth;
- char *question_to;
- char *answer_a;
- char *answer_b;
- char *answer_c;
- char *answer_d;
- char *answer_r;
- char *cat;
- //enum right_answer solution;
- } Question;
- int main()
- {
- char filename[] = "loim.csv";
- char *line = NULL; /* pointer to use with getline () */
- ssize_t read = 0; /* characters read by getline () */
- size_t n = 0; /* number of bytes to allocate */
- Question **questions = NULL; /* ptr to array of stuct question */
- char *sp = NULL; /* start pointer for parsing line */
- char *p = NULL; /* end pointer to use parsing line */
- int field = 0; /* counter for field in line */
- int cnt = 0; /* counter for number allocated */
- int it = 0; /* simple iterator variable */
- FILE *kerdes = fopen(filename, "r");
- if (!kerdes) {
- fprintf(stderr, "failed to open\n");
- return 1;
- }
- questions = (Question*) malloc(MAXQ * sizeof(Question));
- while ((read = getline(&line, &n, kerdes)) != -1) {
- sp = p = line;
- field = 0;
- //printf("%s\n", sp);
- questions[cnt] = (Question*) malloc(sizeof(Question));
- // Cuts
- while (*p != '\0') {
- if (*p == ',') {
- *p = 0;
- if (field == 0) questions[cnt]->nth = atoi(sp);
- if (field == 1) questions[cnt]->question_to = strdup(sp);
- if (field == 2) questions[cnt]->answer_a = strdup(sp);
- if (field == 3) questions[cnt]->answer_b = strdup(sp);
- if (field == 4) questions[cnt]->answer_c = strdup(sp);
- if (field == 5) questions[cnt]->answer_d = strdup(sp);
- if (field == 6) questions[cnt]->answer_r = strdup(sp);
- if (field == 7) questions[cnt]->cat = strdup(sp);
- /*{
- if (*sp == 'A') {
- questions[cnt]->solution = 0;
- } else if (*sp == 'B') {
- questions[cnt]->solution = 1;
- } else if (*sp == 'C') {
- questions[cnt]->solution = 2;
- } else if (*sp == 'D') {
- questions[cnt]->solution = 3;
- }
- }*/
- *p = ',';
- sp = p + 1;
- field++;
- }
- p++;
- }
- cnt++;
- }
- fclose(kerdes);
- if(line) free(line);
- printf("\nQ\'s: \n\n");
- while (questions[it]) {
- 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);
- it++;
- }
- printf("\n");
- /*
- //srand(time(0));
- //it = rand()%MAXQ;
- //
- //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);
- */
- it = 0;
- while (questions[it]) {
- if (questions[it]->question_to) {
- free(questions[it]->question_to);
- }
- free(questions[it]);
- it++;
- }
- if (questions) {
- free(questions);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement