Redirecting cerr

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Agoston Bejo

    Redirecting cerr

    Hi,
    how can one redirect std::cerr into a file? Specifically, if I have an
    ofstream object, is there something I can do to make "cerr << ..." calls
    write into that particular ofstream object?

    Thx,
    Agoston


  • Dietmar Kuehl

    #2
    Re: Redirecting cerr

    Agoston Bejo wrote:[color=blue]
    > how can one redirect std::cerr into a file?[/color]

    I have answered this question loads of times in the past: you can
    replace 'std::cerr's stream buffer. However, since 'std::cerr' is
    possibly accessed after exiting 'main()', you probably need to
    restore the original stream buffer or allocate your stream buffer
    on the heap.

    Specifically, if I have an[color=blue]
    > ofstream object, is there something I can do to make "cerr << ..."[/color]
    calls[color=blue]
    > write into that particular ofstream object?[/color]

    | std::ofstream out(...);
    | std::streambuf orig = std::cerr.rdbuf (out.rdbuf());
    | ... // use redirected cerr
    | std::cerr.rdbuf (orig);
    --
    <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.c om> - Software Development & Consulting

    Comment

    • Alex Vinokur

      #3
      Re: Redirecting cerr


      "Agoston Bejo" <gusz1@freemail .hu> wrote in message news:crlhok$qoe $1@news.caesar. elte.hu...[color=blue]
      > Hi,
      > how can one redirect std::cerr into a file? Specifically, if I have an
      > ofstream object, is there something I can do to make "cerr << ..." calls
      > write into that particular ofstream object?
      >
      > Thx,
      > Agoston
      >
      >[/color]

      Look at
      * http://groups-beta.google.com/group/...4204134e9956a0
      * http://alexvn.freeservers.com/s1/download.html (Redirecting cout/cerr <--> file )


      --
      Alex Vinokur
      email: alex DOT vinokur AT gmail DOT com






      Comment

      Working...