Using ofstream

This is a discussion on Using ofstream within the C++ Programming forums, part of the General Programming Boards category; hi... this is regarding print to a text file...i can print to a console window but when i try to ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Using ofstream

    hi...

    this is regarding print to a text file...i can print to a console window but when i try to print the same thing to a text file...i don't get anything in the text file after execution...

    i have a class called Decision...just concerned code here...
    Code:
    class Decision {
    friend ostream& operator<<(ostream &o, Decision &d);  // Print out the tree
    Node *root;                     // The top of the decision tree
    };
    this is how i print out the tree structure to the console window:
    Code:
    // Print out the tree
    ostream& operator<<(ostream &o, Decision &d)
    {
       d.root->print(0);
    
       return o;
    }

    then in the main function:
    Code:
    Decision decision("sunburn.examples");
       
       decision.build_tree();               //to build a decision tree
       cout << decision;
    
       ofstream fout;                          //to write out to an output file
       fout.open ("outputfile.txt");
       fout << decision << endl;
       fout << flush;
       fout.close();

    so basically cout << decision <<endl; works fine but the fout << decision bit does not print anything to the text file...

    any ideas?

    Farooq

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by xErath
    I would think the call to close would force the stream to flush to disk?
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    hey thanks a lot...actually i figured it out...see the flushing thing doesn't affect it because i was only looking for the end result of my decision...

    the problem was in my print function...i implemented the ofstream code not in the main function but in the print function of Node...

    thanks a lot anyway...

    Regards,

    Farooq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  2. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 10:40 AM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 07:34 AM

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