Reading random line from a text file
Hello,
I really need help with this program. It's a wheel of fortune program. I think I'm really close to finishing it but I really need help adapting it to read a text file of any length and to make sure that when a random line is chosen from the text file it is not chosen again. Thanks again in advance.
Code:
int initialize_array(char phrase[], char puzzle[], char clue[]){
FILE* phraseFile; /*input file*/
char temp[100]; /*temporary array*/
int line; /*line from which word and clue are chosen*/
size_t i=0; /*counter for loop*/
int len; /*length of line from file*/
line=rand()%43+1; /*randomization of line*/
phraseFile = fopen("clues.txt", "r"); /*opens file*/
if (!phraseFile) {
fprintf(stderr, "Oops - file not opened !\n"); /*if there is error in opening file program is exited*/
exit(1);
}
while(line--){
fgets(temp, 100, phraseFile); /*gets line and stores in temp*/
}
len=strlen(temp);
if(temp[len-1]=='\n')
temp[len-1]=0; /*places the null terminating character after word*/
/*separates clues from word*/
strcpy(clue,strtok(temp, "%"));
/*
strcpy(phrase,strtok(NULL,"%"));*/
char tmp[WORD_LENGTH];
strcpy(tmp,strtok(NULL,"%"));
strcpy(phrase, tmp + 1);
strcpy(puzzle,phrase);
/*strcpy(puzzle,phrase);*/
while(i<strlen(puzzle)){
if(isalpha(puzzle[i])){
puzzle[i] = '*'; /*hides word*/
}
++i;
}
fclose(phraseFile); /*closes file*/
return i;
}