Reading in from file to list<string>?

Hello,

I am learning about lists in class but I do not fully understand to to read in from a file into a list<string> that is a private member of a class. How do you do this?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <list>
#include <string>
#include <fstream>

using namespace std;

class Lexicon {
	private:
		list<string> lexicon;
	public:
		Lexicon();	
		// Constructor that takes in words from a file
		Lexicon(const string& fileName);

		void addWord(const string& str);	
		// Default Constructor
		
	// Overloaded << to print out lexicon
	friend ostream& operator<<(ostream& os, const Lexicon& l);
};

// Default constructor for empty lexicon
Lexicon::Lexicon() {
	lexicon = list<string>();
}

// Constructor that takes in words from file, and stores them in a lexicon.
// If file does not exist, exception is thrown.
Lexicon::Lexicon(const string& fileName) {
	lexicon = list<string>();
}

// Adds word to lexicon if it is not already there
void Lexicon::addWord(const string& str) {
	for(list<string>::iterator it = lexicon.begin(); it != lexicon.end(); it++) {
		if (*it == str) {
			return;
		}
	}
	lexicon.push_back(str);
}


int main()
{
	lexicon test_lex;
	ifstream filename;
	list <string> words;
	
	filename.open("filex");
	if(filename.is_open())
	{
		//Is this correct?
                //adding in file to list<string>
                test_lex.Lexicon(filename);
	}
	else(!filename)
	{
		cout <<"File not found. Terminate. " << endl;
		return 1; 
	}
	
	
}
Last edited on
The design seems a little muddled.

At line 14,
 
    Lexicon(const string& fileName);
the constructor takes a std::string as a parameter.

However in main() at line 56,
 
    test_lex.Lexicon(filename);
the std::ifstream (confusingly called 'filename' is passed as a parameter.

So I think first you need to decide whether to pass a string or a ifstream to the constructor.

Other than that, there are a number of syntax errors, one obvious one is at line 58
else(!filename)
Note that there is no condition needed for an 'else' statement - it should simply read else.
OP: what format is your file in? Can you post it here or a link to it?
@ OP: Here a possible implementation of your Lexicon(string) constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Lexicon::Lexicon( const string& fileName)
{
    // opens the file
    ifstream iFile;
    if (!iFile.open(fileName)) {
        cerr << "at Lexicon::Lexicon(): File couldn't opened.\n";
        return;
    }
    
    string word;
    
    // Reads all words from file. It will be expected
    // that all words are seperated by spaces or line
    // breaks. If not, you must customize that code part.
    while (iFile >> word)
    {
        addWord( word );
    }
    
    // Streams will automatically closed at end of scope.
}
@gunnerfunner my file format is .cpp

@nuderobmonkey thank you, your input answers my question.

The only problem remaining is that I get an 3 errors that says:

In constructor 'Dictionary::Dictionary(const string&)':

[Error] could not convert 'iFile.std::basic_ifstream<_CharT, _Traits>::open<char, std::char_traits<char> >((& filename)->std::basic_string<_CharT, _Traits, _Alloc>::c_str<char, std::char_traits<char>, std::allocator<char> >(), (std::_Ios_Openmode)8u)' from 'void' to 'bool'

[Error] in argument to unary !

I am not sure what to do with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Lexicon::Lexicon(const string& filename)
{
	//lexicon= list<string>();
	//Opens the file
	ifstream iFile;
	if(!iFile.open(filename.c_str()))
	{
		cerr<< "at Lexicon::Lexicon(): File couldn't oepn. \n";
		return;
	}
	
	string words;
	//Reads all word from file
	while (iFile >> words)
	{
		addWord(words);
	}
} 
That error means that iFile.open() returns void (that means nothing), but bool is needed (for evaluating the '!').

Try this:
1
2
3
4
5
6
7
8
9
Lexicon::Lexicon( const string& fileName)
{
    // opens the file
    ifstream iFile( fileName );
    if (!iFile) {
        cerr << "at Lexicon::Lexicon(): File couldn't opened.\n";
        return;
    }
  ...
Topic archived. No new replies allowed.