Originally Posted by
anduril462
How would you do this if, instead of a file, you had the questions written down in a book. Let's say you had a book full of questions, and you wanted to pick two random questions. What numbers would you pick? Well, it depends on how many questions you have, right? So lets say there are 500 questions, and you decide you want to pick questions #37 and #242. How would you locate those questions?
I know how to do it with an array, but now i'm talking about all structure writed in a file.
Code:
struct perg
{
int id;
char ques [100];
char op1 [15];
char op2 [15];
char op3 [15];
char op4 [15];
int res;
}
So my file is writed in binary mode, but each id is unique, when i choose the function see questions, i can see that.
Code:
void ver_perguntas(struct perg *p)
{
int retorno, cont = 0;
FILE *f;
if ((f= fopen("Atividades/gp1.dat", "rb")) == NULL)
{
printf("Erro ao abrir ficheiro\n");
}
retorno = fread(p, sizeof *p, 1, f);
while ( retorno == 1)
{
cont++;
printf("\nId : %d \n", p->id);
printf("\nPergunta : %s", p->ques);
printf("\nOpcao 1 : %s",p->op1);
printf("\nOpcao 2 : %s",p->op2);
printf("\nOpcao 3 : %s",p->op3);
printf("\nOpcao 4 : %s",p->op4);
printf("\nResposta Correta : %d",p->res);
printf("\n");
retorno= fread(p, sizeof *p, 1, f);
}
printf(" \n\n %d perguntas registadas \n", cont);
fclose(f);
}
So what i pretend now is to randomize all my id in that file and give me 2 questions.
step by step
function x()
open the file
read the file
rand id
print some variables.
is not that?
regards