CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: cerr or cout?

  1. #1
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    cerr or cout?

    Whats the difference? My instructor told us to output an error message using "cerr" and regular messages using "cout" but from what I can see, the console output is no different no matter which method you use.

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: cerr or cout?

    output stream and error stream are two very different streams, although both of them are associated with console output by default. However, user is able to redirect any of these two anywhere he wants, and, for example, put all 'normal' messages in console, or "msg.log" file, and all error messages in "error.log" file.

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: cerr or cout?

    I see, thank you But for my purposes in such an introductory class, it doesnt matter which method I use until I get more advanced and get into specifying where I want the outputs for the certain methods to go.

    Thanks!

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: cerr or cout?

    Quote Originally Posted by RaleTheBlade
    I see, thank you But for my purposes in such an introductory class, it doesnt matter which method I use until I get more advanced and get into specifying where I want the outputs for the certain methods to go.

    Thanks!
    Establish good habits from the beginning. MUCH easier than trying to unlearn bad habits. If the message is an error, warning or the like put it to cerr. If it is output that the user would want to see under normal/ideal conditions then put it to cout..

    Doing this properly does not even take one single additional keystroke.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: cerr or cout?

    Actually it is very easy to redirect cout or both but difficult to redirect just cerr.

    By the way, much of the time if an error occurs in your code throw an exception or call a generic logging method (if your function intends to continue). Only at application level should it be decided how to handle errors.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: cerr or cout?

    Quote Originally Posted by NMTop40
    Actually it is very easy to redirect cout or both but difficult to redirect just cerr.
    Not at all.. try this...

    Code:
    C:\>dir SomeFileNameWhichDoesNotExist 2>err.out
    The normal output of a "dir" will still come to the console.
    The error (File Not Found), will NOT appear on the console at all, but will be in the err.out file (and that will be all that is there...)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: cerr or cout?

    You can also re-direct cerr like any other C++ output stream.

    A simple example:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        cerr << "cerr : start" << endl;
        cout << "cout : start" << endl;
    
        // redirect cerr to a file ...
    
        ofstream file("cerr_redirect.txt");
        
        streambuf* strm_buffer = cerr.rdbuf(); // save cerr's output buffer
    
        
        cerr.rdbuf (file.rdbuf());             // redirect output into the file
    
        cerr << "cerr : middle" << endl;       // will go to file
        cout << "cout : middle" << endl;
    
        
        cerr.rdbuf (strm_buffer);              // restore old output buffer
    
    
        cerr << "cerr : finish" << endl;       // back to normal
        cout << "cerr : finish" << endl;
    
        return 0;
    }

  8. #8
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: cerr or cout?

    I see I see... And I do use cerr << variable; whenever I want to output something thats related to an error.

    I cant wait till I get into the more complex stuff like stored procedures, .dll's, pointers, vectors, iterators, things like that. I'm still working on manipulating two dimensional arrays and file input output. Though I'm taking classes, so its a structured learning process and you gotta start from the bottom. Thanks for all the help guys!

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: cerr or cout?

    And how to redirect cerr on UNIX / Linux from the command line?

  10. #10
    Join Date
    Dec 2005
    Posts
    642

    Arrow Re: cerr or cout?

    Quote Originally Posted by NMTop40
    And how to redirect cerr on UNIX / Linux from the command line?
    In bash it's
    Code:
    ls SomeFileNameWhichDoesNotExist 2> err.out

  11. #11
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: cerr or cout?

    Various redirections:
    Code:
    [linux#funkypromptline]:myapp 2> error.log - redirect cerr
    [linux#funkypromptline]:myapp 2> error.log > msg.log - redirect cout and cerr to other files
    [linux#funkypromptline]:myapp > loglog.log 2>&1 - redirect cout and cerr to one file
    However, they might work only with bash, and vary with other shells.

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  12. #12
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: cerr or cout?

    Thanks I will try it. It is useful for purify sessions. If you direct everything you cannot see what is happening in the session, if you do not direct you have to read the error messages from the screen when they are better logged. Ideally you want to redirect cerr alone.

    This worked in bash but not in standard csh which is what I usually use.

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: cerr or cout?

    Quote Originally Posted by NMTop40
    Thanks I will try it. It is useful for purify sessions. If you direct everything you cannot see what is happening in the session, if you do not direct you have to read the error messages from the screen when they are better logged. Ideally you want to redirect cerr alone.

    This worked in bash but not in standard csh which is what I usually use.
    hence the reasonfor the "tee" command in Unix/Linux....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: cerr or cout?

    Hi,
    I use ksh shell and those commands works too. However I use tee utility to store in a file:
    $app 2>&1 | tee <filename.txt>
    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured