Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirecting cout/cerr <--> file : streambuf-method

623 views
Skip to first unread message

Alex Vinokur

unread,
Dec 22, 2002, 3:41:09 AM12/22/02
to
// ============================================
// Redirecting cout/cerr <--> file
// streambuf-method
// Sample of usage
// --------------------------------------------
// Alex Vinokur
// mailto:ale...@go.to
// http://www.simtel.net/pub/oth/19088.html
// --------------------------------------------
// 2002-12-22
// ============================================


// =======================
// Windows 2000
// MinGW 2.0.0.-2
// GNU gcc/g++ version 3.2
// =======================


#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;


// ------------
void sample1 ();
void sample2 ();
// ------------


// ------------
void sample1 ()
{
const string sample_tag ("Sample-1 : ");

streambuf* save_sbuf_fout;
streambuf* save_sbuf_ferr;
streambuf* save_sbuf_cout;
streambuf* save_sbuf_cerr;

ofstream fout;
ofstream ferr;


fout.open ("f1-out.txt");
assert (fout);

ferr.open ("f1-err.txt");
assert (ferr);


save_sbuf_cout = cout.rdbuf();
assert (save_sbuf_cout);

save_sbuf_cerr = cerr.rdbuf();
assert (save_sbuf_cerr);

save_sbuf_fout = fout.rdbuf();
assert (save_sbuf_fout);

save_sbuf_ferr = ferr.rdbuf();
assert (save_sbuf_ferr);

// ------ 1 ------
cout << sample_tag << "AAA to cout" << endl;
cerr << sample_tag << "AAA to cerr" << endl;
fout << sample_tag << "AAA to fout" << endl;
ferr << sample_tag << "AAA to ferr" << endl;
// ---------------


// ------ 2 ------
cout.rdbuf(save_sbuf_fout);
cerr.rdbuf(save_sbuf_ferr);

cout << sample_tag << "BBB to cout" << endl;
cerr << sample_tag << "BBB to cerr" << endl;
fout << sample_tag << "BBB to fout" << endl;
ferr << sample_tag << "BBB to ferr" << endl;

cout.rdbuf(save_sbuf_cout);
cerr.rdbuf(save_sbuf_cerr);
// ---------------


// ------ 3 ------
cout << sample_tag << "CCC to cout" << endl;
cerr << sample_tag << "CCC to cerr" << endl;
fout << sample_tag << "CCC to fout" << endl;
ferr << sample_tag << "CCC to ferr" << endl;
// ---------------


// ------ 4 ------
fout.ostream::rdbuf(save_sbuf_cout);
ferr.ostream::rdbuf(save_sbuf_cerr);

cout << sample_tag << "DDD to cout" << endl;
cerr << sample_tag << "DDD to cerr" << endl;
fout << sample_tag << "DDD to fout" << endl;
ferr << sample_tag << "DDD to ferr" << endl;

fout.ostream::rdbuf(save_sbuf_fout);
ferr.ostream::rdbuf(save_sbuf_ferr);
// ---------------


// ------ 5 ------
cout << sample_tag << "EEE to cout" << endl;
cerr << sample_tag << "EEE to cerr" << endl;
fout << sample_tag << "EEE to fout" << endl;
ferr << sample_tag << "EEE to ferr" << endl;
// ---------------


fout.close();
assert (fout);

ferr.close();
assert (ferr);

}


// ------------
void sample2 ()
{
const string sample_tag ("Sample-2 : ");
streamsize ssize;
string str;

streambuf* save_sbuf_fout;
streambuf* save_sbuf_ferr;
streambuf* save_sbuf_cout;
streambuf* save_sbuf_cerr;

filebuf fout2;
filebuf* fout = new filebuf();
filebuf* ferr = new filebuf();


fout->open ("f2-out.txt", ios::out);
assert (fout->is_open());

ferr->open ("f2-err.txt", ios::out);
assert (ferr->is_open());


save_sbuf_cout = cout.rdbuf();
assert (save_sbuf_cout);

save_sbuf_cerr = cerr.rdbuf();
assert (save_sbuf_cerr);

save_sbuf_fout = fout;
assert (save_sbuf_fout);

save_sbuf_ferr = ferr;
assert (save_sbuf_ferr);


// ------ 1 ------
cout << sample_tag << "AAA to cout" << endl;
cerr << sample_tag << "AAA to cerr" << endl;

str.clear(); str = sample_tag + "AAA to fout\n";
ssize = fout->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());

str.clear(); str = sample_tag + "AAA to ferr\n";
ssize = ferr->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());
// ---------------


// ------ 2 ------
cout.rdbuf(save_sbuf_fout);
cerr.rdbuf(save_sbuf_ferr);

cout << sample_tag << "BBB to cout" << endl;
cerr << sample_tag << "BBB to cerr" << endl;

str.clear(); str = sample_tag + "BBB to fout\n";
ssize = fout->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());

str.clear(); str = sample_tag + "BBB to ferr\n";
ssize = ferr->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());

cout.rdbuf(save_sbuf_cout);
cerr.rdbuf(save_sbuf_cerr);
// ---------------


// ------ 3 ------
cout << sample_tag << "CCC to cout" << endl;
cerr << sample_tag << "CCC to cerr" << endl;

str.clear(); str = sample_tag + "CCC to fout\n";
ssize = fout->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());

str.clear(); str = sample_tag + "CCC to ferr\n";
ssize = ferr->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());
// ---------------


// ------ 4 ------
fout = static_cast<filebuf*> (save_sbuf_cout);
ferr = static_cast<filebuf*> (save_sbuf_cerr);

cout << sample_tag << "DDD to cout" << endl;
cerr << sample_tag << "DDD to cerr" << endl;

str.clear(); str = sample_tag + "DDD to fout\n";
ssize = fout->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());

str.clear(); str = sample_tag + "DDD to ferr\n";
ssize = ferr->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());

fout = static_cast<filebuf*> (save_sbuf_fout);
ferr = static_cast<filebuf*> (save_sbuf_ferr);
// ---------------


// ------ 5 ------
cout << sample_tag << "EEE to cout" << endl;
cerr << sample_tag << "EEE to cerr" << endl;

str.clear(); str = sample_tag + "EEE to fout\n";
ssize = fout->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());

str.clear(); str = sample_tag + "EEE to ferr\n";
ssize = ferr->sputn (str.c_str(), str.size());
assert (static_cast<size_t>(ssize) == str.size());
// ---------------


fout->close();
assert (!fout->is_open());

ferr->close();
assert (!ferr->is_open());

delete fout;
delete ferr;
}


// ------------
int main ()
{
sample1();
sample2();

return 0;
}

0 new messages