Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems in reading a file 1

Status
Not open for further replies.

dst222

Programmer
Oct 29, 2003
1
CA
Hi,

The following is a function inside a class BST (binary search tree) What I trying to do is to read an input file which contains a collection of user ids and passwords and store each pair of id/password into different node of a binary search tree.

The problem is that all the insert(i,p); calls are done after the while loop is finished...so all the nodes of my tree contain the same user id and password which is the last user id and password in the input file.

I think it might be because insert is a recusive function...I don't know I'm not sure please help!

void read()
{
//temporary variables to handle user id and password
char *i,*p;
//initialize user id and password
i=new char[8];
p=new char[7];

//open the input file
ifstream in("userids.dat");



/***********************************************/
//THIS LOOP IS NOT WORKING
//ALL I GET IS THE LAST LINE OF THE INPUT FILE
//WHY ?????????????????
/***********************************************/


//priming read
in.get(i,8);

//loop until we reach the end of file character
while(!in.eof())
{
//ignore all spaces between the user id and password
in.eatwhite();

//get the user password
in.get(p,7);

//go to the next line in the input file
in.ignore(360,'\n');


/*****************************************************/
//UNCOMMENT THE FOLLOWING LINE AND YOU'LL
//SEE THAT I'M READING THE RIGHT INFO
//cout<<&quot;info: '&quot;<<i<<&quot;' '&quot;<<p<<&quot;'&quot;<<endl;
/****************************************************/


//create and insert a new node with a user id and password
root=inserthelp(root,i,p);

//read the next user id from input file
in.get(i,8);
}
//close the input file
in.close();
}//end of add


just in case you guys would like to see the inserthelp function...here it is:


inserthelp(BinNode<Elem>* subroot, const Elem& val, const Elem& p) {
if (subroot == NULL) // Empty tree: create node
{
return (new BinNodePtr<Elem>(val, p, NULL, NULL));
}
if (EEComp::lt(val, subroot->val())) // Insert on left
subroot->setLeft(inserthelp(subroot->left(), val, p));
else subroot->setRight(inserthelp(subroot->right(), val, p));
return subroot; // Return subtree with node inserted
}
 
First : the inserts are NOT done after the while-loop
Second: it's easy to debug and you should start off
checking root on the first call to inserthelp
 
Is &quot;root&quot; initialized to null? Also, add some output statements to your insert algorithm so you can follow along. YOu could also add a static counter to the function to see how deep into the recursion you go.

Matt
 
> tree contain the same user id and password which is the last user id and password in the input file
...
> root=inserthelp(root,i,p);
I'm guessing you store i and p directly (the pointers) rather than what they point at.

For each new node you create in the tree, you need to do
Code:
node->i = new char[8]; strcpy( node->i, i );
ditto for p

Otherwise, ALL your nodes just end up pointing at the same bit of memory (the memory you allocated in read())
The end result of this is that all your nodes appear to contain the same data, because they're all pointing at the same memory location.



--
 

Your root node should be initialized once. Why do you have this:

//create and insert a new node with a user id and password
root=inserthelp(root,i,p);

Every time you add an new item you return it so root points to it.

Here's some code that reads a file and adds to a tree that might help you out. Just check out the AddPersonToTree() member function. Notice pRootNode is NULLed in the constructor also. I am creating a tree of CPersonInfo objects but same logic you're looking for.


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRicksTreeCtrl::CRicksTreeCtrl():
pRootNode(NULL),
iDisplayIndex(0)
{

}

CRicksTreeCtrl::~CRicksTreeCtrl()
{

}

/*****************************************************************************

GetFileData()

*****************************************************************************/
void CRicksTreeCtrl::GetFileData()
{
int iSetRoot = 1;

file.open(&quot;Lab_5_Data_File.txt&quot;);

// read line of file
while(!file.eof()){
// get last name
file >> cTextIn;
strcpy(cPersonInfo.cPlayerName,cTextIn);
strcpy(NameListForDisplay[iDisplayIndex],cPersonInfo.cPlayerName);
iDisplayIndex++;

// get goals
file >> cPersonInfo.iGoals;

// get assists
file >> cPersonInfo.iAssists;

// get points
file >> cPersonInfo.iPoints;

// add person information
AddPersonToTree();
}
file.close();

GetUserInput();
}

/*****************************************************************************

AddPersonToTree()

*****************************************************************************/
void CRicksTreeCtrl::AddPersonToTree()
{
CPersonNode *temp, *parent = NULL;

temp = pRootNode;

while(temp != NULL){
parent = temp;
if(strcmp(cPersonInfo.cPlayerName, temp->nodeValue.cPlayerName) == 0){
cout << &quot;MATCHED &quot; << cPersonInfo.iGoals << endl;
return;
}
else if(strcmp(cPersonInfo.cPlayerName, temp->nodeValue.cPlayerName) < 0)
temp = temp->left;
else
temp = temp->right;
}

pNewNode = new CPersonNode(cPersonInfo, NULL, NULL);

if(parent == NULL)
pRootNode = pNewNode;
else if(strcmp(cPersonInfo.cPlayerName, parent->nodeValue.cPlayerName) < 0)
parent->left = pNewNode;
else
parent->right = pNewNode;
}

/*****************************************************************************

DisplayTree()

*****************************************************************************/
void CRicksTreeCtrl::DisplayTree()
{
DisplayTree(pRootNode, &quot;\t&quot;);
}

/*****************************************************************************

DisplayTree(CPersonNode* pNodeIn, char* Seperator)

*****************************************************************************/
void CRicksTreeCtrl::DisplayTree(CPersonNode* pNodeIn, char* Seperator)
{
if(pNodeIn != NULL){
DisplayTree(pNodeIn->left, Seperator);
cout << pNodeIn->nodeValue.cPlayerName << Seperator;
cout << pNodeIn->nodeValue.iGoals << Seperator;
cout << pNodeIn->nodeValue.iAssists << Seperator;
cout << pNodeIn->nodeValue.iPoints << Seperator << endl;
DisplayTree(pNodeIn->right, Seperator);
}
}

/*****************************************************************************

SearchList(char *pName)

*****************************************************************************/
void CRicksTreeCtrl::SearchList(CPersonNode* pNodeIn, char* Seperator, char *pName)
{
if(pNodeIn != NULL){
SearchList(pNodeIn->left, Seperator,pName);
if(strcmp(pName,pNodeIn->nodeValue.cPlayerName) == 0){
cout << pNodeIn->nodeValue.cPlayerName << Seperator;
cout << pNodeIn->nodeValue.iGoals << Seperator;
cout << pNodeIn->nodeValue.iAssists << Seperator;
cout << pNodeIn->nodeValue.iPoints << Seperator << endl;
return;
}
SearchList(pNodeIn->right, Seperator, pName);
}
}

/*****************************************************************************

GetUserInput()

*****************************************************************************/
void CRicksTreeCtrl::GetUserInput()
{
int iSelection;

do{
int iIndex = 0;
cout << &quot;\n&quot;;
do{
cout << iIndex << &quot;) &quot; << NameListForDisplay[iIndex] << endl;
iIndex++;
}while(iIndex < iDisplayIndex);

cout << iIndex << &quot;) &quot; << &quot;Show all...&quot; << endl;

cout << &quot;\nSelect a score to search for (ctrl + c to exit): &quot;;
cin >> iSelection;
cout << &quot;\n&quot;;

if(iSelection > iIndex)
cout << &quot;INPUT ERROR\n&quot;;
else if(iSelection == iIndex)
DisplayTree(pRootNode, &quot;\t&quot;);
else
SearchList(pRootNode, &quot;\t&quot;, NameListForDisplay[iSelection]);
}while(1);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top