0

I'm making quiz program in c and part of my program is to getting questions from text file, count and print them. But when i want to print or count them every time it sees whitespace it skips to other variable. How can i solve it?

That's my txt file

What is the Capital of France? Paris Roma London Istanbul Belgrad

And how it seems in my program.

What
a)is
b)the
c)capital
d)of
e)France

That's how i get questions

    FILE *fp = fopen("fp.txt", "a+");
    gets(questions[n].question_part);        
    gets(questions[n].a);
    gets(questions[n].b);
    gets(questions[n].c);
    gets(questions[n].d);
    gets(questions[n].e);
    questions[n].answer=getch();
    fprintf(fp, "%s %s %s %s %s %s %c", questions[n].question_part, questions[n].a, questions[n].b, questions[n].c, questions[n].d, questions[n].e, questions[n].answer);
    n++;

How i count

    int x=0;
    while(!feof(fp)){   
    fscanf(fp, "%s %s %s %s %s %s %c", questions[x].question_part, questions[x].a, questions[x].b, questions[x].c, questions[x].d, questions[x].e, questions[x].answer);
    x++;}
    n=x;

And how i print

    FILE *fp;           
    fp = fopen("fp.txt", "r");
    int y;
    for(y=0;y<n;y++) 
    {       
        printf("\nQuestion number %d:\n",y+1);
        printf("Question: %s\n",questions[y].question_part);
        printf("a)%s\n",questions[y].a);
        printf("b)%s\n",questions[y].b);
        printf("c)%s\n",questions[y].c);
        printf("d)%s\n",questions[y].d);
        printf("e)%s\n",questions[y].e);
        printf("Answer: %c\n",questions[y].answer);
    } 
Envy
  • 9
  • 3
  • 2
    First of all, never ***ever*** use `gets`! It's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function that have even been removed in the C specifications. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) to read lines instead. – Some programmer dude Nov 22 '19 at 10:14
  • you need to use another seperator than a whitespace when writing to and reading from file, otherwise there is no way the fscanf functionality will work, – MikNiller Nov 22 '19 at 10:18
  • Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Nov 22 '19 at 10:19

3 Answers3

0

The problem is that the %s format for fscanf reads space delimited strings.

I suggest you solve this by writing the question and the options on separate lines. For example by

fprintf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%c\n", questions[n].question_part, questions[n].a, questions[n].b, questions[n].c, questions[n].d, questions[n].e, questions[n].answer);

Now you can read each line with fgets.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Try using another seperator, e.g. '_' change

 fprintf(fp, "%s %s %s %s %s %s %c", questions[n].question_part, questions[n].a, questions[n].b, questions[n].c, questions[n].d, questions[n].e, questions[n].answer);

to

fprintf(fp, "%s_%s_%s_%s_%s_%s_%c", questions[n].question_part, questions[n].a, questions[n].b, questions[n].c, questions[n].d, questions[n].e, questions[n].answer);

and

fscanf(fp, "%s %s %s %s %s %s %c", questions[x].question_part, questions[x].a, questions[x].b, questions[x].c, questions[x].d, questions[x].e, questions[x].answer);

to

fscanf(fp, "%s_%s_%s_%s_%s_%s_%c", questions[x].question_part, questions[x].a, questions[x].b, questions[x].c, questions[x].d, questions[x].e, questions[x].answer);

if you want to keep one question with answers on one line, otherwise i think you should go with the answer that suggest printing each part on a newline.

MikNiller
  • 1,242
  • 11
  • 17
0

Alternatively, if you can not control the input file, consider using the '?' as the terminator the question (assuming this will hold true for all questions).

One proposal: consider using pointer to 'current' question - q in the following exapmle, it will make code easier.

struct question *q ;   // whatever structure name describe the question/answer.
q = &question[x] ;
fscanf(fp, "%[^?]? %s %s %s %s %s %c",
    q->question_part, 
    q->a, q->b, q->c, q->d, q->e, q->answer);

dash-o
  • 13,723
  • 1
  • 10
  • 37