Thread: ostream and ofstream

  1. #1
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    ostream and ofstream

    I have been designing a class and trying to come up with a better way of handling error output. If a file is specified, I want all errors to be written to the file, if no file is specified, I want the errors to be written to cerr. I have been using something like this:
    Code:
    // errfile is declared as ostream* errfile
    if (file_given) {
        errfile = new ofstream(filename);
    }
    else {
        errfile = &cerr;
    }
    Is this a valid way of handling the error output?

    Also in the class destructor, I do this:
    Code:
    if (errfile != &cerr) {
        delete static_cast<ofstream*>(errfile);
    }
    Any advice would be appreciated?

    Thanks,
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This thread looks at what you are trying to do.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another Novice Question - ofstream
    By C++Gamer in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2006, 08:23 AM
  2. Serialization
    By Asagohan in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2005, 10:57 PM
  3. passing an ofstream by reference
    By quizkiwi in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2005, 09:58 PM
  4. Using ofstream
    By alvifarooq in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2004, 09:06 PM
  5. Deriving from ostream...
    By thynctank in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 03:39 PM