Can't reopen a file with different words

I'm having to build a Hangman game and within the game I've developed a void function that will open the file and read the next text. The problem I'm having is it opens and reads the first file fine, but when it moves to the second line/word it crashes. The code in question is below and any help would be greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  void open_and_hide(char word_string[10], int& letters_of_word, char solution[])
{

    char next;
    int index = 0;

    
    word_file.open("wordlist.dat");
    while (next != '\n')
    {
        
        word_file.get(next);
        word_string[index] = next;
        index++;
        
    }

    cout<<word_string;
    
    
    for (int i = 0; i < (index-1); i++)
    {
        solution[i] = '*';
        
    }
    cout<<solution;
    letters_of_word = index-1;
    word_file.close()

}
You appear to have a global file object. Move it into the function and make it local.

As it's been used before, it's in the wrong state. You really shouldn't have any global variables at all.
Now I have it locally within the function this will still not read the second line. It reads the first line again as if it were the second line. Not sure how to keep the file open continually throughout the program so when it calls the function again it will start on line 2, then 3, etc...
At a glance:

char next is uninitialized before it is used in the while loop

And each time you will grab the same line, when you close the file and open it back up again you will start from the beginning, making your function read the first line and closing it.

Another note, using those char arrays to hold words is a bit iffy, use char* or even better, std::string
I don't know what way to keep the file open throughout the entire program so when the main function repeatedly calls the open_and_hide function it will continue where it left off if that makes sense.
Hmmm you would have to do some tedious work setting the filestream pointer. May I suggest something to you?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <fstream>
#include <string>

// This function will open a file and read the contents into a list
// The function takes a filename and a std::vector of type string

void ReadFile(std::string filename, std::vector<std::string>& WordList)
{
	std::ifstream ifs(filename.c_str()); // Open the file
        std::string temp;		                   // We will read in each word to this string
	while(ifs.good())                              // While we can still read stuff
	{
		
		std::getline(ifs, temp);		// Get each word from the file
		if(temp == "") break;			// If nothing is found exit the loop
		WordList.push_back(temp);		// Add the word to the list

	}
	ifs.close();						// Make sure to close the file after reading!
}

int main()
{
	std::vector<std::string> Words;		// Make a list for the words to go in
	ReadFile("words.txt", Words);		// Read a file into the list

	// This is just a fancy for loop, called a "Range based for loop"
	// I learned it today and want to put it in practice :)
	// Just imagine for(i = 0; i < Words.size(); i++)
	for(const auto& i : Words) std::cout << i << "\n"; // Print out the words

	// Of course you can make an index, and as they complete them you can move onto the next word

	unsigned int Index = 0;

	std::string word = Words[Index];

	// Play the game
	// If they guessed it, go to the next word
	Index++;
	if(Index >= Words.size())
	{
		std::cout << "Thank you for playing!\n\n";
		return 0;
	}
}
Topic archived. No new replies allowed.