Hi all,
I am having a problem with a double link list.
The list is loaded from a file, I got the list to move forward but cant seem to move backward.
Account.h:
Code:#include <iostream> #include <cstring> using namespace std; class Account { int age; public: Account(){age=0;}; Account(int a){age=a;}; int retAge(){return age;}; Account *next; Account *prev; };Code:main.cpp: #include <cstdlib> #include <iostream> #include <cstring> #include "Account.h" #include <fstream> using namespace std; void loadFile(); void saveFile(int); Account *currP; //current account pointer Account *rootP;//root wont change int main(int argc, char *argv[]) { saveFile(12); saveFile(13); saveFile(14); loadFile(); system("PAUSE"); return EXIT_SUCCESS; } void saveFile(int yr) { fstream sfile("Account.txt",ios::binary|ios::app|ios::out); if(!sfile.is_open()) sfile.open("Account.txt",ios::binary|ios::out); Account nAcnt(yr); sfile.write(reinterpret_cast<char*>(&nAcnt),sizeof(Account)); sfile.close(); } void loadFile() { fstream ofile("Account.txt",ios::binary|ios::in); if(!ofile.is_open()) return ; // rootP=new Account; Account *tempAcnt; int sizeAcnt=sizeof(Account); ofile.read(reinterpret_cast<char*>(rootP),sizeAcnt); currP=rootP; rootP->prev=0; //points to nothing cout<<"Root Next Pointer: " <<rootP->retAge()<<endl; //check output int count=1; while(!ofile.eof()) { tempAcnt=new Account; currP->next=new Account; currP->prev=new Account; cout<<"loop: "<<count<<endl; ofile.read(reinterpret_cast<char*>(tempAcnt),sizeAcnt); cout<<"currP-retAge() : "<<currP->retAge()<<endl; currP->prev = currP; currP->next = tempAcnt; currP=tempAcnt; cout<<"new currP-retAge() : "<<currP->retAge()<<endl; count++; } ofile.close(); currP=rootP; //reset point to root while(currP->retAge()!=0) { cout<<"current Previous Pointer: "<<currP->retAge()<<endl; currP=currP->next; } currP=currP->prev; //problem is at either this line or the next cout<<currP->retAge(); //line cause returning age cause an error return ; }



LinkBack URL
About LinkBacks


