output tree to file?

This is a discussion on output tree to file? within the C++ Programming forums, part of the General Programming Boards category; i'm trying to output a tree to a file. here's what i was trying: Code: save(treenode *treeroot, ofstream outfile){ if(!treeroot) ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    85

    output tree to file?

    i'm trying to output a tree to a file. here's what i was trying:

    Code:
    save(treenode *treeroot, ofstream outfile){
        if(!treeroot) return;    
        save(treeroot->left,outfile);
        oufile<<treeroot->data;
        save(treeroot->right,outfile);
    }
    
    int main(){
        treenode *treeroot;
        
        //do stuff
    
        ofstream outfile("file.txt",ios::out);
        save(treeroot, outfile);
    
    }
    the error i got was ios is private, or something like that.
    how can i get this to work, or is there a better way to do this?

    another thing i was wondering, is there a way to output to a file without deleteing the data already in the file?
    for example: if i had a file file.txt with the data from the tree in it, and then i ran the program and added stuff to the tree, can i open file.txt and add only the new element of the tree, or do i have to outfile the whole tree again?

    thanks.
    Last edited by qwertiop; 12-24-2001 at 02:53 PM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    ofstream

    ios::app will open the file for append... a nifty trick is to open the file for append and if it fails the first time open it for out so that it will get created. The other mention is that you are recreating an ofstream object each time that you call the save function. I would try a pointer instead it should work.

    I would redeclare the function like this:

    save(treenode *treeroot, ofstream* outfile){
    if(!treeroot) return;
    save(treeroot->left,outfile);
    *oufile<<treeroot->data;
    save(treeroot->right,outfile);
    }

    Also the tree root type must be global and have all members public for outfile to be able to access it. The first call to the save function will look like this

    save( treeroot, &outfile)

    Sometime after the file must be closed.
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21