Skip to content
Prev 2230 / 10988 Next

[Rcpp-devel] rout and rerr instead of cout and cerr in Rcpp

Just to follow up myself, it turns out that it?s easy to redirect cerr and
cout to files, and that solves all my issues.
In case anyone is interested, here is how I did it:


#include<ostream>
#include<fstream>

RcppExport SEXP myfunction(SEXP params)
{
    try
    { // or use BEGIN_RCPP macro

        //start off by redirecting
        streambuf* save_sbuf_fout;
        streambuf* save_sbuf_ferr;
        streambuf* save_sbuf_cout;
        streambuf* save_sbuf_cerr;
        ofstream fout;
        ofstream ferr;
        fout.open("cout.txt");
        ferr.open("cerr.txt");
        save_sbuf_cout = cout.rdbuf();
        save_sbuf_cerr = cerr.rdbuf();
        save_sbuf_fout = fout.rdbuf();
        save_sbuf_ferr = ferr.rdbuf();
        cout.rdbuf(save_sbuf_fout);
        cerr.rdbuf(save_sbuf_ferr);

        /*do whatever I normally do and write to cerr and/or cout all I
want*/

        //revert to original state
        cout.rdbuf(save_sbuf_cout);
        cerr.rdbuf(save_sbuf_cerr);
        fout.close();
        ferr.close();

    }
}
On 4/27/11 9:26 PM, "Sean Robert McGuffee" <sean.mcguffee at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110427/11044a42/attachment.htm>