
Originally Posted by
sean_cantab
(b) the eof() however seems required, otherwise the program just reads the first line
That doesn't make sense, until you erroneously changed your code from:
Code:
if (f_ptr->is_open()) {
while (!f_ptr->eof()) {
string line;
getline(*f_ptr, line);
stringstream stream(line);
copy(istream_iterator<string>(stream), istream_iterator<string>(), back_inserter(words));
}
}
to:
Code:
if (f_ptr->is_open()) {
string line;
getline(*f_ptr, line);
stringstream stream(line);
copy(istream_iterator<string>(stream), istream_iterator<string>(), back_inserter(words));
}
in which case of course the program just reads the first line, not because you're not calling eof(), but because you're not calling getline in a loop. What I proposed would be:
Code:
if (f_ptr->is_open()) {
string line;
while (getline(*f_ptr, line)) {
stringstream stream(line);
copy(istream_iterator<string>(stream), istream_iterator<string>(), back_inserter(words));
}
}
in which case the program will read line by line, contrary to your claim that eof() is required otherwise the program just reads the first line. Actually, since you are going to parse for "words" anyway, you don't need getline at all. You might as well write:
Code:
if (f_ptr->is_open()) {
copy(istream_iterator<string>(*f_ptr), istream_iterator<string>(), back_inserter(words));
}

Originally Posted by
Elysia
I do not see any open() in your original pasted code.
Line 16.