I'm trying to read in word by word (and keep track of line numbers -- that's why I'm using getline but that's a separate problem) from a text file and getting a repeat of the last line of the file. I think it's because I'm using getline() but I'm setting a new node every time but when I displayAll() it prints only the last line of the file. (example of some file and error at bottom). I included the Node class because that's most relevant to this issue whereas LinkedListOfFiles is mostly functions to support the full list of words once created.
class Node
{
public:
string getNode(){return node;}
void setNode(string s){node=s;}
string getFile(){return file;}
void setFile(string s){file=s;}
int getNodeNumber(){return nodeNumber;}
void setNodeNumber(int ln){if(ln<0)ln=0;nodeNumber=ln;}
friend class LinkedListOfFiles;
int number;
char name;
Node * next;
Node() {pLeft=NULL;pRight=NULL;}
Node(Node * pS) {pLeft=NULL;pRight=NULL;pData=pS;}
friend class BSTOfWords;
private:
Node * pData;
Node * pLeft;
Node * pRight;
//Node * pData;
Node * pNext;
Node * pPrev;
string node;
string file;
int nodeNumber;
};
void LinkedListOfFiles::addFile(string fileName)
{
int line = 0;
ifstream InputFile;
InputFile.open (fileName);
string w = "",next;
Node * wnode = new Node;
(*wnode).setFile(fileName);
while (!InputFile.eof())
{
getline(InputFile,next);
(*wnode).setNode(next);
line++;
if (next == "\n"){cout<<"eof found! \n";}
(*wnode).setNodeNumber(line);
putAtFront (wnode);
cout << (*wnode).getFile()+" "+intToString((*wnode).getNodeNumber())+" "+(*wnode).getNode()+" \n";
Node * wnode = new Node;
wnode->pData = wnode->pNext;
}
//cout << " outbound to file list: "+(*wnode).getFile()+" \n";
}
void LinkedListOfFiles::putAtFront(Node * ps )
{
insert(ps,pFront);
}
void LinkedListOfFiles::insert(Node * pNewWords, Node * pFound)
{
Node * pNewNode;
pNewNode = new Node;
(*pNewNode).pData=pNewWords;
(*((*pFound).pNext)).pPrev=pNewNode;
(*pNewNode).pNext=(*pFound).pNext;
(*pNewNode).pPrev=pFound;
(*pFound).pNext=pNewNode;
}
string LinkedListOfFiles::displayAll()
{
string result;
// pointer to current node
Node * pCurrentNode;
// make current node the first item in list
pCurrentNode = (*pFront).pNext; //pFront points to the sentinal, it's pNext points to the first item in list
while((*pCurrentNode).pData != NULL)
{
result+= (*((*pCurrentNode).pData)).getFile(); //add the currrent node's fileName
result+=" ";
result+= intToString((*((*pCurrentNode).pData)).getNodeNumber()); //add the currrent node's lineNumber
result+=" ";
result+= (*((*pCurrentNode).pData)).getNode(); //add the currrent node's line of text
result+=" ";
result+= "\n";
pCurrentNode = (*pCurrentNode).pNext;
}
return result; // return the string with all the data
}
Here's a file and an example of the error it would generate
Some say the world will end in fire,
Some say in ice.
From what I've tasted of desire
I hold with those who favour fire.
But if it had to perish twice,
I think I know enough of hate
To say that for destruction ice
Is also great
And would suffice.
And the error is
test.txt 9 And would suffice.
test.txt 9 And would suffice.
test.txt 9 And would suffice.
test.txt 9 And would suffice.
test.txt 9 And would suffice.
test.txt 9 And would suffice.
test.txt 9 And would suffice.
test.txt 9 And would suffice.
test.txt 9 And would suffice.
I'm trying to learn C++ and linked lists right now. These may be similar (if they help you in reviewing this issue) Linked List From Text File want to read more than 50,000 txt files and save them in linked list in C++ but are different because I may not be using the pointers properly to keep each node as I advance (some pointers may point back to self).