Lewiky Lewiky - 1 month ago 7
C Question

How can I read in a random string from a large list of words in C?

I'm developing a basic implementation of the game hangman.

I have a text file of the most 1000 common English language words and I'd like to just choose one of those at random to be the word in my game.

I know I need to use fopen and fscanf to get the word into a string in my program, but how do I choose which string to read in?

Would I have to import the whole list into an array and then choose one from there? Or is there a way I can just select which word to scan in?

Answer

The easy way is to put in in an array, then get a random index. The more complex, but efficient, is to read an index of each word, save to another file, and then, just read the sector where that word is stored. Really, just put it in an array.

Comments