I am a newbie in C++. I'm currently doing a task that requires me to read a file, and store each line into a linked list, and then store all linked lists into a linked list. In sum, I would need to create a linked list of rows, and each row contains linked list of chars.
And I am stuck on here for quite a while. As the program will be tested by files that have different number of lines. So that I am thinking that I am supposed to create multiple linked lists according to the actual number of lines. However, my current solution cannot accomplish this.
Below is my loadFile method
void loadFile(list< <list<char> > &rows, const string &file)
{
list<char> elements;
char ch;
ifstream fin;
fin.open(file.c_str());
while(!fin.eof()){
while(fin.get(ch))
{
if(ch != '\n')
{
elements.push_back(ch);
}
rows.push_back(elements);
}
}
fin.close();
}
Apparently, this method does not do what I am supposed to do. So I am wondering is there anyway that I could create as many as linked list according to the number of lines in the file, and push all linked lists into the row linked list? Or could anyone please advise my alternative solution? (The file has to be stored into linked lists tho, which means we cannot use array etc.) Appreciate for your help in advance.