C%2b%2b Ostream Dev Null

971 // with rawnullostream, but it's better to have rawnullostream follow 972 // the rules than to change the rules just for rawnullostream.

C 2b 2b Ostream Dev Null Command

-->

Null Ostream Class in C Code #include class NulStreambuf: public std:: streambuf char dummyBuffer 64; protected: virtual int overflow (int c) setp. Them ofstream('/dev/null') but this makes my program less portable. Is there a null ostream built in cpp? Is it easy to implement one? You can try o.clear(ios::failbit), and then any statement o NULL in case someone calls o.rdbuf and writes to the buffer directly.

C 2b 2b Ostream Dev Null Key

Defines the class template basic_ostream, which mediates insertions for the iostreams. The header also defines several related manipulators. (This header is typically included for you by another of the iostreams headers. You rarely need to include it directly.)

Syntax

C%2b%2b Ostream Dev Null

Typedefs

Type nameDescription
ostreamCreates a type from basic_ostream that is specialized on char and char_traits specialized on char.
wostreamCreates a type from basic_ostream that is specialized on wchar_t and char_traits specialized on wchar_t.

Manipulators

NameDescription
endlTerminates a line and flushes the buffer.
endsTerminates a string.
flushFlushes the buffer.
swapExchanges the values of the left basic_ostream object parameter for those of the right basic_ostream object parameter.

Operators

OperatorDescription
operator<<Writes various types to the stream.

Classes

ClassDescription
basic_ostreamThe class template describes an object that controls insertion of elements and encoded objects into a stream buffer.

See also

Header Files Reference
Thread Safety in the C++ Standard Library
iostream Programming
iostreams Conventions

Ondra Holub wrote:
toton napsal:
>Hi,
I want to have a log stream, where I can put certain logs. The log
can be console or file or any other ui widget.
This part I had already done with. However I want log stream
(std::ostream type) to initialize to a default null stream, when it is
not explicitly set to other stream, which will consume all of the
characters, like /dev/null .
How to write such a stream, derived from basic_ostream ?

Use ordinary std::fstream, open it only for writing to required file
'/dev/null'. It should work.
This would work on unices, but it is platform-specific.
If you really want to create own stream, just derive it from
basic_ostream and simply define your own operator<< to be function
which only returns stream reference. You will have to write dummy
'write' and 'put' method too (and all other methods for output).
This would be better, as it is not platform specific.
However, my suggestion would be to subclass std::streambuf to ignore any
characters written. Then the log stream can start out with the 'null'
streambuf, but be provided with a different one when logging is desired.
--
Nate