Importing using fstream and loading into Set

Feb 27, 2013 at 5:59pm
Im trying to import a text file which holds all dictionary words to be used to solve an algorithm to predict words by adding one letter to the beginning, and one letter to the end.

My problem is before even beginning on how to write the logic for this I can't seem to load the dictionary into a set. We are supposed to use a set. I have read over STL's and sample programs but haven't made much progress... Here is what I have.

1
2
3
4
	fstream dictFile;
	dictFile.open("words.txt", fstream::in);
	std::set<string> Dictonary;
	Dictonary.insert(dictFile);
Feb 27, 2013 at 6:13pm
You can't insert a fstream into your set since it is defined as set<string>.
You need to read dictfile a record at a time and insert each word into the set.
1
2
3
string word;
while (dictfile >> word)
    Dictonary.insert(word);

This assumes one word per dictfile record.
Feb 28, 2013 at 3:01am
Each line has a word, this doesn't seem to load properly seeing how the line "Press any key to continue" pops up immediately. A dictionary of this size (4.7mb) should take a little bit to load into a set right??

Here is what I have

1
2
3
4
5
6
	while (getline(dictFile,word))
	{
	std::getline(dictFile, word);
                Dictonary.insert(word);
	}
Last edited on Feb 28, 2013 at 3:02am
Feb 28, 2013 at 2:03pm
Bumping....
Feb 28, 2013 at 2:13pm
The point is that you need to open the file properly. you need to check is_open() for example.

The issue is: does "words.txt" exists where the program expects it.

Especially in debug mode you need to set the 'Execution working dir' (in case of Code::Blocks) in order to find the file
Last edited on Feb 28, 2013 at 2:14pm
Feb 28, 2013 at 2:25pm
is_open() returns true. Am I reading the file into the <set> correctly??
Feb 28, 2013 at 2:32pm
remove the second getline() on line 3. Then yes, it should work. AbstractionAnon solution should work as well...

just cout << word << endl; to see if you got the words
Topic archived. No new replies allowed.