pointers - C++ Linked List -- how to read without repeating to same nodes -
i'm trying read in word word (and keep track of line numbers -- that's why i'm using getline that's separate problem) text file , getting repeat of last line of file. think it's because i'm using getline() i'm setting new node every time when displayall() prints last line of file. (example of file , error @ bottom). included node class because that's relevant issue whereas linkedlistoffiles functions support 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 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 current node node * pcurrentnode; // make current node first item in list pcurrentnode = (*pfront).pnext; //pfront points sentinal, it's pnext points first item in list while((*pcurrentnode).pdata != null) { result+= (*((*pcurrentnode).pdata)).getfile(); //add currrent node's filename result+=" "; result+= inttostring((*((*pcurrentnode).pdata)).getnodenumber()); //add currrent node's linenumber result+=" "; result+= (*((*pcurrentnode).pdata)).getnode(); //add currrent node's line of text result+=" "; result+= "\n"; pcurrentnode = (*pcurrentnode).pnext; } return result; // return string data }
here's file , example of error generate
some world end in fire, in ice. i've tasted of desire hold favour fire. if had perish twice, think know enough of hate destruction ice great , suffice.
and error is
test.txt 9 , suffice. test.txt 9 , suffice. test.txt 9 , suffice. test.txt 9 , suffice. test.txt 9 , suffice. test.txt 9 , suffice. test.txt 9 , suffice. test.txt 9 , suffice. test.txt 9 , suffice.
i'm trying learn c++ , linked lists right now. these may similar (if in reviewing issue) linked list text file want read more 50,000 txt files , save them in linked list in c++ different because may not using pointers keep each node advance (some pointers may point self).
the loop in addfile function should be:
while(getline(inputfile, next).good()) { // stuff }
if getline reaches end of file value in next unchanged. cause of repeated line.
Comments
Post a Comment