How does one suppress stdout?

Does anyone know how to suppress a cout or printf? I do not want anything
printed to the screen. I cannot redirect output to /dev/null because I am
spawning the process. I have tried doing “close (STDOUT_FILENO)” but I
think that is not the right way to do it. Any suggestions?

Rex

“Rex Lam” <Rex.Lam@igt.com> wrote in message
news:9sde10$24c$1@inn.qnx.com

Does anyone know how to suppress a cout or printf? I do not want anything
printed to the screen. I cannot redirect output to /dev/null because I am
spawning the process. I have tried doing “close (STDOUT_FILENO)” but I
think that is not the right way to do it. Any suggestions?

For printf and cout:

FILE* check = freopen("/dev/null", “w”, stdout);

For std::cout only:
#include
class nullbuf: public std::streambuf
{
};

//…
nullbuf devnull;
std::streambuf* oldcout = std::cout.rdbuf(&devnull);
// now cout goes nowhere.
// and vitally before exit of main to avoid crash, sigsegv or whatever:
std::cout.rdbuf(oldcout);

Note that std::cout will be in bad or fail state if you do this.
std::cout.bad() will return true, and std::cout.good() will return false.
You can get around this if you are willing to put up with a performance hit
by changing nullbuf to this:

class nullbuf: public std::streambuf
{
protected:
virtual int overflow(int c)
{
return c; // pretend output was successful
}
};

In the above version full output formatting will occur, which is fairly
expensive. With the first version, no formatting of output will occur.

Tom

Tom,

Thanks for your help. It works great. Any suggestions on disabling stdin?

Rex

“Tom” <tom.widmer@camcog.com> wrote in message
news:9se2fs$fof$1@inn.qnx.com

“Rex Lam” <> Rex.Lam@igt.com> > wrote in message
news:9sde10$24c$> 1@inn.qnx.com> …
Does anyone know how to suppress a cout or printf? I do not want
anything
printed to the screen. I cannot redirect output to /dev/null because I
am
spawning the process. I have tried doing “close (STDOUT_FILENO)” but I
think that is not the right way to do it. Any suggestions?

For printf and cout:

FILE* check = freopen("/dev/null", “w”, stdout);

For std::cout only:
#include <streambuf
class nullbuf: public std::streambuf
{
};

//…
nullbuf devnull;
std::streambuf* oldcout = std::cout.rdbuf(&devnull);
// now cout goes nowhere.
// and vitally before exit of main to avoid crash, sigsegv or whatever:
std::cout.rdbuf(oldcout);

Note that std::cout will be in bad or fail state if you do this.
std::cout.bad() will return true, and std::cout.good() will return false.
You can get around this if you are willing to put up with a performance
hit
by changing nullbuf to this:

class nullbuf: public std::streambuf
{
protected:
virtual int overflow(int c)
{
return c; // pretend output was successful
}
};

In the above version full output formatting will occur, which is fairly
expensive. With the first version, no formatting of output will occur.

Tom

“Rex Lam” <Rex.Lam@igt.com> wrote in message
news:9seiam$pqu$1@inn.qnx.com

Tom,

Thanks for your help. It works great. Any suggestions on disabling stdin

What behaviour do you want for disabled? Always at eof? Hmm…

Try exactly the same as you did for cout:
nullbuf devnull;
std::streambuf* oldcinbuf = std::cin.rdbuf(&devnull);
// std::cin now always at eof I think
// don’t forget to reset:
std::cin.rdbuf(oldcinbuf);

An alternative for both is to have:
std::filebuf devnull; // just don’t open a file!

This just saves you from writing a nullbuf class, but is a little more
obfuscated I think.

A final useful class is (perhaps you can think of a better name):

class BufSwapper
{
public:
BufSwapper(std::ios_base& stream, std::streambuf* newbuf)
:m_old(stream.rdbuf(newbuf)), m_stream(stream)
{
}
~BufSwapper()
{
m_stream.rdbuf(m_old);
}
private:
std::streambuf* m_old;
std::ios_base& m_stream;
};

Now you can do:

BufSwapper bufswap(std::cout, &devnull);
// when bufswap goes out of scope it resets cout for you.

Tom

“Tom” <> tom.widmer@camcog.com> > wrote in message
news:9se2fs$fof$> 1@inn.qnx.com> …

“Rex Lam” <> Rex.Lam@igt.com> > wrote in message
news:9sde10$24c$> 1@inn.qnx.com> …
Does anyone know how to suppress a cout or printf? I do not want
anything
printed to the screen. I cannot redirect output to /dev/null because
I
am
spawning the process. I have tried doing “close (STDOUT_FILENO)” but
I
think that is not the right way to do it. Any suggestions?

For printf and cout:

FILE* check = freopen("/dev/null", “w”, stdout);

For std::cout only:
#include <streambuf
class nullbuf: public std::streambuf
{
};

//…
nullbuf devnull;
std::streambuf* oldcout = std::cout.rdbuf(&devnull);
// now cout goes nowhere.
// and vitally before exit of main to avoid crash, sigsegv or whatever:
std::cout.rdbuf(oldcout);

Note that std::cout will be in bad or fail state if you do this.
std::cout.bad() will return true, and std::cout.good() will return
false.
You can get around this if you are willing to put up with a performance
hit
by changing nullbuf to this:

class nullbuf: public std::streambuf
{
protected:
virtual int overflow(int c)
{
return c; // pretend output was successful
}
};

In the above version full output formatting will occur, which is fairly
expensive. With the first version, no formatting of output will occur.

Tom
\