Print to nowhere with ostream

I would like to send data to nowhere, I mean that I do not want to print data in the console or in a file, but I need some kind of std::ostream . How to do it?

+8
c ++ stream printing
source share
5 answers

I used:

 std::ostream bitBucket(0); 

recently without problems, although it was noted as having some potential problems if you looked at it from a certain angle (see link below).

In addition: From what I understand (and I'm not quite sure about this), this call above ultimately ends with a call to basic_ios::init(0) , and since it skips the NULL pointer, it sets the state of the stream returned by the function rdstate() , set to badbit .

This, in turn, does not allow the stream to display any additional information, but simply discards it.

The following program shows this in action:

 #include <iostream> int main (void) { std::ostream bitBucket(0); bitBucket << "Hello, there!" << std::endl; return 0; } 

The page on which I got it also showed this as a solution with a cleaner solution (slightly modified to remove duplication of my first solution above):

 #include <iostream> class null_out_buf : public std::streambuf { public: virtual std::streamsize xsputn (const char * s, std::streamsize n) { return n; } virtual int overflow (int c) { return 1; } }; class null_out_stream : public std::ostream { public: null_out_stream() : std::ostream (&buf) {} private: null_out_buf buf; }; null_out_stream cnul; // My null stream. int main (void) { std::cout << std::boolalpha; //testing nul std::cout << "Nul stream before: " << cnul.fail() << std::endl; cnul << "Goodbye World!" << std::endl; std::cout << "Nul stream after: " << cnul.fail() << std::endl; } 
+10
source share

The simplest solution is to simply output to unopened std::ofstream (or any other output stream in an error state). This will cause the thread to be constantly in an error state. This may be an advantage ( << will skip formatting), but if there is any code that you cannot control error checking and does something specific, if you are likely to have problems.

Otherwise, it is quite simple to implement an empty stream; the only streambuf function you really need to override is overflow . Something like the following should do the trick:

 class NulStreambuf : public std::streambuf { char dummyBuffer[64]; protected: virtual int overflow( int c ) { setp( dummyBuffer, dummyBuffer + sizeof( dummyBuffer ) ) ; return (c == EOF) ? '\0' : c ; } }; 

(The buffer will avoid unnecessary calls to virtual functions. Platforms, this is significant.)

Then create an output stream that uses it:

 class NulOStream : public NulStreambuf, public std::ostream { public: NulOStream() : std::ostream( this ) {} }; 

(Using inheritance rather than restraint ensures that streambuf is completely designed before being transferred to ostream . In practice, this is not required at all, but the standard does not seem to allow the transfer of an streambuf to the ostream constructor.)

+5
source share

The simplest solution: use std::stringstream .

 #include <sstream> #include <iostream> void func(std::ostream& o){ o << "blatest\n"; } int main(){ std::stringstream black_hole; func(std::cout); func(black_hole); } 

stringstream will contain the output, but if you do not use it, it will be the same as if it were not filled.

+2
source share

Some suggestions here http://bytes.com/topic/c/answers/589209-std-null-stream

Good answer from this site.

Use regular std :: fstream, open it only for writing to the required file "/ DEV / zero". It should work.

If you really want to create your own stream, simply derive it from basic_ostream and simply define your own operator <to be a function that returns only a link to the stream. You will need to write the dummy 'write' and 'put' as well (and all other methods for output).

Really,

 #include <streambuf> #include <ostream> template <class cT, class traits = std::char_traits<cT> > class basic_nullbuf: public std::basic_streambuf<cT, traits> { typename traits::int_type overflow(typename traits::int_type c) { return traits::not_eof(c); // indicate success } }; template <class cT, class traits = std::char_traits<cT> > class basic_onullstream: public std::basic_ostream<cT, traits> { public: basic_onullstream(): std::basic_ios<cT, traits>(&m_sbuf), std::basic_ostream<cT, traits>(&m_sbuf) { init(&m_sbuf); } private: basic_nullbuf<cT, traits> m_sbuf; }; typedef basic_onullstream<char> onullstream; typedef basic_onullstream<wchar_t> wonullstream; 

From http://bytes.com/topic/c/answers/428285-null-ostream

0
source share

Since no one mentioned this, if it suppresses std output or errors, you can simply close the corresponding file descriptors (e.g. fclose (stdout) or fclose (stderr) ).
This will joke everything, including things like printf or fprintf (stderr, ...)
This way you will actually use regular cout or cerr , but they will be turned into battle buckets.

0
source share

All Articles