我试图从文本文件逐字读取(并跟踪行号-这就是为什么我使用getline,但这是一个单独的问题),并获得文件的最后一行的重复。我认为这是因为我使用getline(),但我每次都设置一个新节点,但当我displayAll()时,它只打印文件的最后一行。(一些文件的例子和底部的错误)。我包含Node类是因为它与这个问题最相关,而LinkedListOfFiles主要是支持创建后的完整单词列表的函数。
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
}
下面是一个文件和它将产生的错误示例
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.
错误是
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.
我现在正在学习c++和链表。这些可能是相似的(如果他们帮助你回顾这个问题)链接列表从文本文件想要读取超过50,000个文本文件,并将它们保存在c++的链接列表中,但不同的是,因为我可能没有正确使用指针来保持每个节点,因为我前进(一些指针可能指向返回self)。
addFile函数中的循环应该是:
while(getline(InputFile, next).good())
{
// do stuff
}
如果getline到达文件的末尾,那么next中的值将保持不变。这可能是重复行的原因。