I have a txt file containing a bunch of words, one per line. I need to read this file and put each word in a list then the user will be able to modify this list when done editing, the program will write the modified list in a new file.
Since it's object orinted c++, I'm gonna have two classes, one to read/write to file, and one to edit/mess with the list and the user.
with this approach in mind, here's my read function from the first class:
bool FileMgr::readToList(list<string> &l)
{
if(!input.is_open())
return false;
string line;
while(!input.eof())
{
getline(input, line);
l.push_back(line);
}
return true;
}
keep in mind: input is opened at constructor. questions: is there a less redundant way of getting that damn line from istream and pushing it back to l? (without the 'string' in between). questions aside, this functions seems to work properly.
now the output function:
bool FileMgr::writeFromList(list<string>::iterator begin, list<string>::iterator end)
{
ofstream output;
output.open("output.txt");
while(begin != end)
{
output << *begin << "\n";
begin++;
}
output.close();
return true;
}
this is a portion of my main:
FileMgr file;
list<string> words;
file.readToList(words);
cout << words.front() << words.back();
list<string>::iterator begin = words.begin();
list<string>::iterator end = words.end();
file.writeFromList(begin, end);
thanks for the help, both functions now work. Now regarding style, is this a good way to implement these two functions? also the getline(input,line) part I really don't like, anyone got a better idea?