-2

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?

1
  • its my code i get some help but then no one answers so i started again! @MikeCAT Dec 5, 2015 at 1:48

1 Answer 1

-1

You throw away the all remaining data in the file by while (fgets(str, 500, haidar)!= NULL);. Remove the harmful line and instead try this:

if (fgets(str, sizeof(str), haidar)== NULL){

    printf("error\n");
}
strcpy(all_ques[i].correctanswer, str);    // store the correct answer
fgets(str, sizeof(str), haidar); // read one line (and throw it away)

Since there doesn't seem any member to store the blank lines and "Its in Sweden!", it will be thrown away.

Also note that

  • 10-byte array is too small to store the choices. You have to allocate more memory.
  • The member correctanswer of struct quiz has just one byte and is too small for storeing string which contains 1 characters or more. You have to increase the size of array.
  • You should use sizeof(str) instead of the magic number 500 in the arguments of fgets().
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.