Redirecting cerr

A

Agoston Bejo

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
 
D

Dietmar Kuehl

Agoston said:
how can one redirect std::cerr into a file?

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
ofstream object, is there something I can do to make "cerr << ..." calls
write into that particular ofstream object?

| std::eek:fstream out(...);
| std::streambuf orig = std::cerr.rdbuf(out.rdbuf());
| ... // use redirected cerr
| std::cerr.rdbuf(orig);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top