C%2b%2b Ostream Dev Null

C (Cpp) ostream - 30 examples found. These are the top rated real world C (Cpp) examples of std::ostream extracted from open source projects. You can rate examples to help us improve the quality of examples. An Ostream is an abstract base class for all output systems (streams, files, token lists. Or run find / -type f -name iostream 2 /dev/null grep include or locate iostream grep include (provided the database is current, otherwise prepend with a call to updatedb)-these, however, will print also non-system-wide includes, so please adjust appropriately. The actual C include path is easily found with something like.

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
I often make C++ classes that write to some stream given to them:
Bear::Save(std::ostream& out) {
out << 'Fur is ' << color << std::endl;
out << 'Age is ' << age << std::endl;
}

Editing somebody's code today, I needed an ostream equivalent of /dev/null, some stream into which a class could write without printing anything. This can be done by creating a stream buffer that never prints.
Nullclass nullbuf : public std::basic_streambuf
{
protected:
virtual int overflow(int c) { return c; }
};
class nullstream : public std::basic_ostream > {
nullbuf _streambuf;
public:
nullstream() :
std::basic_ostream &gt(&_streambuf)
{ clear(); }
};
Ostream
Using it looks like any other stream:

C 2b 2b Ostream Dev Null Code

Null
nullstream dev_null;
dev_null << 'Nobody will ever see this.';
ostream* output = new nullstream();
(*output) << 'This will be invisible.';

C 2b 2b Ostream Dev Null Command

As usual, hope this helps.

Comments are closed.