1

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).

Community
  • 1
  • 1
stackuser
  • 869
  • 16
  • 34
  • 1
    First, don't knock yourself for using std::getline(). The alternatives are almost all hideous, and it is a most-awesome standard library function. That said, **do** knock yourself for using `.eof()`. It is almost *never* correct in a loop eval condition, and my immediate take is this is no exception to that rule. See [this question and answer list](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for more information as to why. – WhozCraig Apr 09 '13 at 05:34
  • The error is almost certainly in the insert method, which has to be some of the most complicated pointer code I've seen. It's far too complicated for a simple linked list. Anyway, `pFound` seems to be quite important but nothing in your post explains what it is or how it gets it's value. Post more on that please. – john Apr 09 '13 at 05:42
  • @john, i don't see anymore references to pFound in the entire project. seems to only make an appearance as a parameter to insert(). do you think that insert() is the source of the error? i had insert() from a previous program that worked to insert into a linked list but seems to have issues in this case. – stackuser Apr 09 '13 at 06:37
  • 1
    @stackuser, If pFound is not being used anywhere else then that is a definite error. I would rewrite insert from scratch for your new program. – john Apr 09 '13 at 07:14

1 Answers1

1

The loop in the addFile function should be:

while(getline(InputFile, next).good())
{
    // do stuff
}

If getline reaches the end of the file then the value in next will be unchanged. This is likely the cause of the repeated line.

Zacrath
  • 521
  • 3
  • 8
  • It's interesting. What that does is starts by skipping the 1st line and then skips every other line in reading/printing (so only 1/2 of the lines printed). And the original issue remains. – stackuser Apr 09 '13 at 05:41
  • @stackuser did you remove the getline() from the body of the loop? It should only be in the while-condition. You probably have other issues, and I'll glance at those now, but should make sure this is fixed. – WhozCraig Apr 09 '13 at 05:42
  • //while (!InputFile.eof()) while ( getline(InputFile,next).good()) { //getline(InputFile,next); InputFile >> next; (*wnode).setNode(next); that's the way looks now and what this this does is just reads the 1st word of the line because i'm using >> operator. not sure but maybe i should putting this into some loop with an if(next=='\n') then but not sure of the something. anyhow it doesn't matter because the original issue remains. – stackuser Apr 09 '13 at 05:49