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
Typedefs
Type name | Description |
---|---|
ostream | Creates a type from basic_ostream that is specialized on char and char_traits specialized on char . |
wostream | Creates a type from basic_ostream that is specialized on wchar_t and char_traits specialized on wchar_t . |
Manipulators
Name | Description |
---|---|
endl | Terminates a line and flushes the buffer. |
ends | Terminates a string. |
flush | Flushes the buffer. |
swap | Exchanges the values of the left basic_ostream object parameter for those of the right basic_ostream object parameter. |
Operators
Operator | Description |
---|---|
operator<< | Writes various types to the stream. |
Classes
Class | Description |
---|---|
basic_ostream | The 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
toton napsal:This would work on unices, but it is platform-specific.>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.
If you really want to create own stream, just derive it fromThis would be better, as it is not platform specific.
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).
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