std::cerr

Discussion in 'C++' started by Dylan, Jun 7, 2005.

  1. Dylan

    Dylan Guest

    What advantages does std::cerr offer over std::cout?

    (I'm trying to understand why it exists)

    thanks
     
    Dylan, Jun 7, 2005
    #1
    1. Advertising

  2. Dylan

    Lionel B Guest

    Dylan wrote:
    > What advantages does std::cerr offer over std::cout?


    There is no "advantage".

    > (I'm trying to understand why it exists)


    It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
    log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
    terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
    MISSION ABORT!".

    --
    Lionel B
     
    Lionel B, Jun 7, 2005
    #2
    1. Advertising

  3. Dylan

    Dylan Guest

    On Tue, 7 Jun 2005 10:35:28 +0100, "Lionel B" <> wrote:

    >Dylan wrote:
    >> What advantages does std::cerr offer over std::cout?

    >
    >There is no "advantage".
    >
    >> (I'm trying to understand why it exists)

    >
    >It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
    >log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
    >terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
    >MISSION ABORT!".


    hehe. So how do I redirect the output of std::cerr, say, to a file?
     
    Dylan, Jun 7, 2005
    #3
  4. Dylan

    Guest

    Dylan <> wrote:
    | What advantages does std::cerr offer over std::cout?

    On my OS, a common thing to do is to make programs reading from cin and
    writing to cout. If foo and bar are two such programs, and I wrote

    foo < infile | bar > outfile

    then foo would have cin connected to the file infile, and its cout
    connected to bar's cin. Bar's cout would be connected to the file
    outfile.

    If any of there is an error, an error message written to cerr would
    appear on the screen immediately.

    Cerr is not buffered, though, so excessive usage might be ineffient. If
    you rather want to write information to the screen, there is also a
    buffered version called clog.

    summary:
    cin buffered, connected to stdin
    cout buffered, connected to stdout
    cerr unbuffered, connected to stderr
    clog buffered, connected to stderr.
    --
    Robert Bauck Hamar
     
    , Jun 7, 2005
    #4
  5. Risto Lankinen, Jun 7, 2005
    #5
  6. wrote:
    > Dylan <> wrote:
    > | What advantages does std::cerr offer over std::cout?
    >
    > On my OS, a common thing to do is to make programs reading from cin and
    > writing to cout. If foo and bar are two such programs, and I wrote
    >
    > foo < infile | bar > outfile
    >
    > then foo would have cin connected to the file infile, and its cout
    > connected to bar's cin. Bar's cout would be connected to the file
    > outfile.
    >
    > If any of there is an error, an error message written to cerr would
    > appear on the screen immediately.
    >
    > Cerr is not buffered, though, so excessive usage might be ineffient. If
    > you rather want to write information to the screen, there is also a
    > buffered version called clog.
    >
    > summary:
    > cin buffered, connected to stdin
    > cout buffered, connected to stdout
    > cerr unbuffered, connected to stderr
    > clog buffered, connected to stderr.


    Wow great!!. What's your OS?
    > --
    > Robert Bauck Hamar
     
    Prawit Chaivong, Jun 7, 2005
    #6
  7. Dylan

    Guest

    Prawit Chaivong <> wrote:
    | wrote:
    | > Dylan <> wrote:
    | > | What advantages does std::cerr offer over std::cout?
    | >
    | > On my OS, a common thing to do is to make programs reading from cin and
    | > writing to cout. If foo and bar are two such programs, and I wrote
    | >
    | > foo < infile | bar > outfile
    | >
    | > then foo would have cin connected to the file infile, and its cout
    | > connected to bar's cin. Bar's cout would be connected to the file
    | > outfile.
    | >
    | > If any of there is an error, an error message written to cerr would
    | > appear on the screen immediately.
    | >
    | > Cerr is not buffered, though, so excessive usage might be ineffient. If
    | > you rather want to write information to the screen, there is also a
    | > buffered version called clog.
    | >
    | > summary:
    | > cin buffered, connected to stdin
    | > cout buffered, connected to stdout
    | > cerr unbuffered, connected to stderr
    | > clog buffered, connected to stderr.
    |
    | Wow great!!. What's your OS?

    None of importance, really; the four objects are standard in C++, but
    I use Crux <URL: http://crux.nu>, Solaris <URL: http://www.sun.com> and
    Redhat Enterprise Linux <URL: http://www.redhat.com/> on a regular
    basis.

    To another question in this thread: I can redirect stderr to a file
    with

    foo 2> errmsg

    (for a program foo and a file errmsg.)
    --
    Robert Bauck Hamar
     
    , Jun 7, 2005
    #7
  8. >> hehe. So how do I redirect the output of std::cerr, say, to a file?
    >The answer is very OS and compiler implementation specific


    Really? Re-assigning stream buffers is a portable operation:

    #include <fstream>
    #include <iostream>

    int main()
    {
    std::eek:fstream log("somefile");
    std::streambuf *save = std::cerr.rdbuf();

    // Redirect stream buffers
    if (log.is_open())
    std::cerr.rdbuf(log.rdbuf());

    std::cout << "This is a test message\n";
    std::cerr << "This is a test message\n";

    // Restore cerr's stream buffer before terminating
    if (log.is_open())
    std::cerr.rdbuf(save);
    }
     
    James Daughtry, Jun 7, 2005
    #8
  9. Dylan

    red floyd Guest

    Lionel B wrote:
    > Dylan wrote:
    >
    >>What advantages does std::cerr offer over std::cout?

    >
    >
    > There is no "advantage".
    >
    >
    >>(I'm trying to understand why it exists)

    >
    >
    > It is frequently convenient to be able to separate "normal" from "abnormal" output. Thus one common use of cerr is to
    > log errors: i.e. one might (re)direct output to stderr (but not to stdout) to an error log, which might be a file, a
    > terminal screen or (my preferred option) a tannoy that squawks "ERROR! ERROR! ILLEGAL MEMORY ACCESS AT 0x038af2e2!
    > MISSION ABORT!".
    >


    Also, cerr is also unbuffered, whereas cout is buffered. clog exists,
    and (as I understand it) is a buffered version of cerr.
     
    red floyd, Jun 7, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike - EMAIL IGNORED

    vc++6.0: capture cerr

    Mike - EMAIL IGNORED, Oct 2, 2003, in forum: C++
    Replies:
    6
    Views:
    634
  2. Trevor
    Replies:
    2
    Views:
    517
  3. Mike Wahler

    cout & cerr

    Mike Wahler, May 24, 2004, in forum: C++
    Replies:
    1
    Views:
    489
    regisser
    May 24, 2004
  4. Agoston Bejo

    Redirecting cerr

    Agoston Bejo, Jan 7, 2005, in forum: C++
    Replies:
    2
    Views:
    10,047
    Alex Vinokur
    Jan 7, 2005
  5. Jim Langston

    Overriding std::cout or std::cerr

    Jim Langston, Aug 14, 2006, in forum: C++
    Replies:
    3
    Views:
    661
    Jim Langston
    Aug 15, 2006
Loading...

Share This Page