Suppress cout output with function

I use a shared library whose functions make std :: cout everywhere. Is it possible to do something at the caller level where I can suppress outout cout or redirect it to some place?

You could even try to do such a thing in C ++.

+5
source share
2 answers

Something like this, just create function wrappers for library calls that will redirect cout.

int main( void )
{
 std::ofstream lStream( "garbage.txt" );
 std::streambuf* lBufferOld = std::cout.rdbuf();

 std::cout.rdbuf( lStream.rdbuf() );
 std::cout << "Calling library function" << std::endl;

 std::cout.rdbuf( lBufferOld );
 std::cout << "Normal output" << std::endl;

 std::cout.rdbuf( lStream.rdbuf() );
 std::cout << "Calling another library function" << std::endl;

 std::cout.rdbuf( lBufferOld );
 std::cout << "Another normal output" << std::endl;

 lStream.close();

 return ( 0 );
}
+4
source

-, . , , , .

- , , .

ostream, , cout. , , , , - , , , .

0

All Articles