0

I have a .txt file named question.txt which contains the multiple-choice questions and multiple answers for them in this format :

**X question content 
# Answer 1
# Answer 2
...
# Answer n
  • X is an integer (a number of the chapter from which the question was taken)
  • n is smaller or equal to 5

I'm trying to extract the information on the chapter number (X) the question content and the answers of said question and store them into a struct variable like so

struct {
    int chapter;
    int qcontent[512];
    char answer[5][256];
}

Below is my attempt I was wondering if there is a different approach to this, maybe a more compact way ?

#include <stdio.h>

typedef struct {
  int chapter;
  char qcontent[512];
  char answer[5][256];
} question;

int main()
{
  question question[100];

  FILE *fp = fopen("question.txt", "r");
  char fline[512];
  int i = -1; // Count question
  int j = 0; // Count answer in a question
  
  while (!feof(fp)) {
    fgets(fline, 512, fp);
    fline[strlen(fline) - 1] = 0;

    if (strstr(fline, "**")) {
      ++i; 
      question[i].chapter = fline[2] - '0';
      strcpy(question[i].qcontent, fline + 4);  
      j = 0; 
    }

    if (strstr(fline, "#")) {
      strcpy(question[i].answer[j++], fline + 2);
    }
  }
    return 0;
}
Kain
  • 329
  • 1
  • 10
  • 1
    [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Jul 13 '21 at 16:37
  • 1
    There's no `group` member in the `question` structure. Did you mean `question[i].chapter`? – Barmar Jul 13 '21 at 16:38
  • `fline[2] - '0'` won't work if the chapter number can be more than 1 digit. – Barmar Jul 13 '21 at 16:39
  • You're missing a `}`, so you have `return 0;` inside the loop and it returns after processing the first line of the file. – Barmar Jul 13 '21 at 16:41

1 Answers1

1

Note: There are many errors in the code you have provided. However, I will demonstrate a way to work with such cases.

First, you need to know how the structure information was saved in the file. Lets say, we have the following format.

chapter, question, ans1, ans2, ans3, ans4, ans5
datatype: int, char*, char*, char*, char*, char*, char*

Meaning, we separate each element by a comma. This can be a problem if your question or answer element contains comma. In this case, just find a symbol like |,\ that are unlikely to be present in your question or answer. The file that we will use to demonstrate the example is this

#include <stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct {
  int chapter;
  char qcontent[512];
  char answer[5][256];
} question;

int main(){
  question question[100];
  FILE *file = fopen("question.txt","r");
  
  char buffer[2048];
  memset(buffer, 2048, 0); // initializing the buffer with 0, its always   a good practice to initialize.
  int i=0;
  while(fgets(buffer, 2048, file)!=NULL){
// atoi() = ascii to integer, a function frob stdlib.h
      question[i].chapter = atoi(strtok(buffer, "\n,"));
      strcpy(question[i].qcontent, strtok(NULL, ","));
      for(int j=0; j<4; j++){
        strcpy(question[i].answer[j], strtok(NULL, ","));
      } 
      // The last string will have a \n next to it, so-
      strcpy(question[i].answer[4], strtok(NULL, ",\n"));
      i++;
  }

  for(int index=0; index<i; index++){
    printf("Question on chapter %d:\n",question[index].chapter);
    printf("%s\n",question[index].qcontent);
    printf("Answers:\n");
    for(int j=0; j<5; j++) printf("%s\n",question[index].answer[j]);
  }
return 0;
}

You will see this output in the screen.

You can learn more here.

How to read/write 2D array from file?

How does strtok() work?

How does fgets() work?

How does atoi() work?

Daoist Paul
  • 133
  • 8
  • 1
    The file format is part of the question. You can't imagine a new one that fits your need. Revise your code to follow the question. – fpiette Jul 13 '21 at 17:19