I'm writing a program that outputs a random question from a file. In the file, I have each question numbered like this 1-5:
1 Question
multiple choices...
2 Question
multiple choices...
etc.
How can I use "random" so the program can jump to that number and print out only that question?
Code:#include <stdio.h> #define DATAFILE "/home/usr/trivia" int main(){ FILE *data = fopen(DATAFILE, "r"); if(data == NULL){ puts("File not found"); return 0; } srand(time(NULL)); int random = rand() % 5 + 1; char text[1000]; int i, j; while(fgets(text, 1000, data) != NULL){ printf("%s", text); } puts(""); fclose(data); return 0; }



LinkBack URL
About LinkBacks


