0

So I have writing to a file and reading from a file. Now I need to be able to find specific data that was previously entered and saved to that certain file. Here is the code I have so far.

{
	ofstream myFile ("words.txt");
	if (myFile.is_open())
	{
		myFile << dictionary.newWord();
		myfile.close();
	}
	else cout << "Cannot open file";
	}

	{
		string word;
		ifstream myFile ("words.txt");
		if (myFile.is_open())
		{
			while (! myFile.eof() )
			{
				getWord (myFile,word);
				cout << word << endl;
			}
			myFile.close();
		}
		else cout << "Cannot open file";
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by ArrogantLegend
0

That doesn't tell much. Appears like the program is just writing one word to the file then attempting to read it back. The open() statement you have will erase everything that's already in the file. If you want to add more words every time you run the program then you have to use ios::ate as the second parameter in that open() statement. ofstream myFile ("words.txt", ios::ate);

0

So for reading the text file do I have to do the same thing?

example:

ifstream myFile ("words.txt", ios::ate);

Like that? Or is there something different?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.