0

Hi, I'm trying to update a file with an information in a binary tree. However my algorithm is working only for one NODE.

Any suggestion will be appreciated!

void Arbol::updateFile()
{
Nodeptr p = miArbol; // myTree
Recorrer(p);
cout << "File updated!" << endl;
cout << endl;
}    

void Arbol::Recorrer(Nodeptr p) 
{ // Recorrer began
 
if (p != NULL)
 { // if began
    Recorrer(p->left);
    fstream fout;
    fout.open("data.txt", ios::out);
    if ( !fout.fail() )
    { // if began
       fout << p->Objperson.nombre << endl; 
       fout << p->Objperson.ss << endl;
       fout << p->Objperson.edad << endl;
    } // if end
    else
    { // else began
       cerr << "File can't be open!" << endl; 
       exit(1);
       system("PAUSE");   
    } // else end
    fout.close(); 
    Recorrer(p->right); 
  } // if end

} // Recorrer end
2
Contributors
4
Replies
7
Views
9 Years
Discussion Span
Last Post by kako13
0
if ( !fout.fail() )
    { // if began
       fout << p->Objperson.nombre << endl; 
       fout << p->Objperson.ss << endl;
       fout << p->Objperson.edad << endl;
    } // if end

That should be a while loop. Also your program will be a little more efficient if you do not use endl because endl does more than print '\n'.

while( p != NULL)
{
       fout << p->Objperson.nombre << '\n'; 
       fout << p->Objperson.ss << '\n';
       fout << p->Objperson.edad << '\n';
    p = p->next; // or whatever pointer you use in that structure
}
0
if ( !fout.fail() )
    { // if began
       fout << p->Objperson.nombre << endl; 
       fout << p->Objperson.ss << endl;
       fout << p->Objperson.edad << endl;
    } // if end

That should be a while loop. Also your program will be a little more efficient if you do not use endl because endl does more than print '\n'.

while( p != NULL)
{
       fout << p->Objperson.nombre << '\n'; 
       fout << p->Objperson.ss << '\n';
       fout << p->Objperson.edad << '\n';
    p = p->next; // or whatever pointer you use in that structure
}

My intention is to take the information that is in the object person that is in the node, then move to the next node recursively (left later right).

It don't work. Why I need a while? The function will be called recursively, that would be iterative. I can't do p = p->next because or other structure because the a node in a binary tree have left & right, that would for a traversal in a linked list.
Any other suggestion?
Thanks

0

If that function is called recursively then you can't open the file for output on every recursive iteration. Open the output file before calling that function the first time and pass it the file stream

void Arbol::updateFile()
{
Nodeptr p = miArbol; // myTree
ofstream fout("filename");
Recorrer(p, fout);
fout.close();
cout << "File updated!" << endl;
cout << endl;
}    

void Arbol::Recorrer(Nodeptr p, ofstream& fout) 
{ // Recorrer began
 
if (p != NULL)
 { // if began
0

If that function is called recursively then you can't open the file for output on every recursive iteration. Open the output file before calling that function the first time and pass it the file stream

void Arbol::updateFile()
{
Nodeptr p = miArbol; // myTree
ofstream fout("filename");
Recorrer(p, fout);
fout.close();
cout << "File updated!" << endl;
cout << endl;
}    

void Arbol::Recorrer(Nodeptr p, ofstream& fout) 
{ // Recorrer began
 
if (p != NULL)
 { // if began

Thanks, now is working like a charm.
Here is the code:

void Arbol::updateFile()
{
Nodeptr p = miArbol;
ofstream fout;
fout.open("data.txt", ios::out);
Recorrer(p,fout);
fout.close(); 
cout << "File updated!" << endl;
cout << endl;
}    

void Arbol::Recorrer(Nodeptr p, ofstream & fout) 
{ // Recorrer began
 
if (p != NULL)
 { // if began
    Recorrer(p->left, fout);
    fout << p->Objperson.nombre << endl;
    fout << p->Objperson.ss << endl;
    fout << p->Objperson.edad << endl;
    Recorrer(p->right, fout); 
 } // if end
    
} // Recorrer end
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.