我正在尝试从文本文件中逐字阅读(并跟踪行号 - 这就是我使用 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 个 txt 文件并将它们保存在 C++ 中的链接列表中, 但它们不同,因为我可能没有正确使用指针来保存每个我前进的节点(一些指针可能指向自我)。