我試圖逐詞閱讀(並跟蹤行號 - 這就是爲什麼我使用getline,但這是一個單獨的問題)從一個單詞文本文件並重新獲得文件的最後一行。我認爲這是因爲我使用getline(),但是我每次都設置一個新節點,但是當我displayAll()時,它只打印文件的最後一行。 (底部的一些文件和錯誤的例子)。我包含了Node類,因爲這個問題與這個問題最爲相關,而LinkedListOfFiles主要是支持創建完成的單詞列表的函數。C++鏈接列表 - 如何閱讀而不重複到相同的節點
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++和鏈表現在。這些可能是相似的(如果他們幫助你審查這個問題)Linked List From Text Filewant to read more than 50,000 txt files and save them in linked list in C++但是不同,因爲我可能沒有正確使用指針保持每個節點,因爲我前進(有些指針可能指向自我)。
首先,不要使用std :: getline()敲你自己。替代品幾乎都是可怕的,它是一個非常棒的標準庫函數。也就是說,** do **敲自己使用'.eof()'。在循環評估條件下,它幾乎不可能是正確的,我的直接看法是這個規則也不例外。請參閱[此問題和答案列表](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong)以獲取更多信息。 – WhozCraig 2013-04-09 05:34:01
這個錯誤幾乎肯定在insert方法中,它必須是我見過的最複雜的指針代碼。對於簡單的鏈接列表來說,這太複雜了。無論如何,'pFound'似乎是相當重要的,但是你的文章沒有解釋它是什麼或者它是如何得到它的價值的。請發佈更多。 – john 2013-04-09 05:42:04
@john,我在整個項目中看不到對pFound的引用。似乎只是將外觀作爲insert()的參數。你認爲insert()是錯誤的根源嗎?我曾插入()從以前的程序工作插入到鏈接列表,但似乎有問題在這種情況下。 – stackuser 2013-04-09 06:37:48