I'm trying to read lines from a text file line by line and store it in an array. the txt file has some question in it which will be asked to the player !
Here's some of the questions !!
1: När kom potatisen till Europa?;A:1300-talet; B:1500-talet; C:900-talet;D:1700-talet\n
rätt svar : B
2: I vilken enhet mats elektrisk spänning ?;A:Ampere;B:Volt;C:Joule;D:Watt\n
Rätt svar: A
3: Från vilket land har vi fått lego?;A:Tyskland;B:Australien;C:Japan;D:Danmark\n
rätt svar : D
Its in Sweden !
I have made a function that splits the line wherever it finds a semicolon! like this:
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct quiz
{
char questions[50];
char* alt[4];
char correctanswer[1];
};
int main(){
struct quiz all_ques[50];
int i = 0;
FILE *haidar;
haidar = fopen("gameee.txt", "r");
char str[500];
char *ptr;
while (fgets(str, 500, haidar)) // read 500 characters
{
ptr = strtok(str, ";"); // split our findings around the " ;"
if(ptr == NULL){
printf("error\n");
}
else {
strcpy(all_ques[i].questions, ptr);// store the question
}
ptr = strtok(NULL, ";"); // and keep splitting
if(ptr == NULL){
printf("error\n");
}
else {
all_ques[i].alt[0] = malloc(10);
strcpy(all_ques[i].alt[0], ptr);// store the first option
}
ptr = strtok(NULL, ";"); // and keep splitting
if(ptr == NULL){
printf("error\n");
}
else{
all_ques[i].alt[1] = malloc(10);
strcpy(all_ques[i].alt[1], ptr); // store the second option
}
ptr = strtok(NULL, ";"); // and keep splitting
if(ptr == NULL){
printf("error\n");
}
else {
all_ques[i].alt[2] = malloc(10);
strcpy(all_ques[i].alt[2], ptr); // store the third option
}
ptr = strtok(NULL, ";"); // and keep splitting
if(ptr == NULL){
printf("error\n");
}
else {
all_ques[i].alt[3] = malloc(10);
strcpy(all_ques[i].alt[3], ptr); // store the fourth option
}
while (fgets(str, 500, haidar)!= NULL);
strcpy(all_ques[i].correctanswer, str); // store the correct answer
i++;
}
}
i can only print out the first question but not the others i and i cant figure it out?