473,543 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

null ostream?

How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?

Nov 23 '05 #1
13 6682
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?


You could create an ofstream that writes to /dev/null
Not portable, but easy :)
Nov 23 '05 #2
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?


I cribbed this from c.l.c++ years ago - don't remember the original
author (but it's not me):

#include <streambuf>
#include <ostream>

template <class cT, class traits = std::char_trait s<cT> >
class basic_nullbuf: public std::basic_stre ambuf<cT, traits> {
typename traits::int_typ e overflow(typena me traits::int_typ e c)
{
return traits::not_eof (c); // indicate success
}
};

template <class cT, class traits = std::char_trait s<cT> >
class basic_onullstre am: public std::basic_ostr eam<cT, traits> {
public:
basic_onullstre am():
std::basic_ios< cT, traits>(&m_sbuf ),
std::basic_ostr eam<cT, traits>(&m_sbuf )
{
init(&m_sbuf);
}

private:
basic_nullbuf<c T, traits> m_sbuf;
};

typedef basic_onullstre am<char> onullstream;
typedef basic_onullstre am<wchar_t> wonullstream;

Best regards,

Tom

Nov 23 '05 #3
try this:

#include <iostream>

struct null_streambuf
: public std::streambuf
{
void overflow(char c)
{
}
};

int main()
{
null_streambuf nullbuf;

// replace buffer of existing stream
std::streambuf * origbuf = std::cout.rdbuf (&nullbuf);
std::cout << "this goes into oblivion";
std::cout.rdbuf (origbuf);

// create null ostream
std::ostream out(&nullbuf);
out << "this goes into oblivion too...";
}
-- peter

Nov 23 '05 #4
to correct myself, Thomas Tutone's version's author hasn't been too
lazy to ignore non-char character traits and the proper return value of
the streambuf::over flow member. use that one. :)

Nov 23 '05 #5

"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream
and
ignores anything that would otherwise be output?


I cribbed this from c.l.c++ years ago - don't remember the original
author (but it's not me):

#include <streambuf>
#include <ostream>

template <class cT, class traits = std::char_trait s<cT> >
class basic_nullbuf: public std::basic_stre ambuf<cT, traits> {
typename traits::int_typ e overflow(typena me traits::int_typ e c)
{
return traits::not_eof (c); // indicate success
}
};

template <class cT, class traits = std::char_trait s<cT> >
class basic_onullstre am: public std::basic_ostr eam<cT, traits> {
public:
basic_onullstre am():
std::basic_ios< cT, traits>(&m_sbuf ),
std::basic_ostr eam<cT, traits>(&m_sbuf )
{
init(&m_sbuf);
}

private:
basic_nullbuf<c T, traits> m_sbuf;
};

typedef basic_onullstre am<char> onullstream;
typedef basic_onullstre am<wchar_t> wonullstream;

Best regards,

Tom


Thanks, Tom! This seems to be exactly what I need:)

Nov 24 '05 #6

"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream
and
ignores anything that would otherwise be output?


I cribbed this from c.l.c++ years ago - don't remember the original
author (but it's not me):

#include <streambuf>
#include <ostream>

template <class cT, class traits = std::char_trait s<cT> >
class basic_nullbuf: public std::basic_stre ambuf<cT, traits> {
typename traits::int_typ e overflow(typena me traits::int_typ e c)
{
return traits::not_eof (c); // indicate success
}
};

template <class cT, class traits = std::char_trait s<cT> >
class basic_onullstre am: public std::basic_ostr eam<cT, traits> {
public:
basic_onullstre am():
std::basic_ios< cT, traits>(&m_sbuf ),
std::basic_ostr eam<cT, traits>(&m_sbuf )
{
init(&m_sbuf);
}

private:
basic_nullbuf<c T, traits> m_sbuf;
};

typedef basic_onullstre am<char> onullstream;
typedef basic_onullstre am<wchar_t> wonullstream;

Best regards,

Tom


May I save the call init(&m_sbuf); in basic_onullstre am's ctor as this
same call is made in basic_ostream's ctor?

Nov 24 '05 #7
Angel Tsankov wrote:

"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?

I cribbed this from c.l.c++ years ago - don't remember the original
author (but it's not me):

#include <streambuf>
#include <ostream>

template <class cT, class traits = std::char_trait s<cT> >
class basic_nullbuf: public std::basic_stre ambuf<cT, traits> {
typename traits::int_typ e overflow(typena me traits::int_typ e c)
{
return traits::not_eof (c); // indicate success
}
};

template <class cT, class traits = std::char_trait s<cT> >
class basic_onullstre am: public std::basic_ostr eam<cT, traits> {
public:
basic_onullstre am():
std::basic_ios< cT, traits>(&m_sbuf ),
std::basic_ostr eam<cT, traits>(&m_sbuf )
{
init(&m_sbuf);
}

private:
basic_nullbuf<c T, traits> m_sbuf;
};

typedef basic_onullstre am<char> onullstream;
typedef basic_onullstre am<wchar_t> wonullstream;

Best regards,

Tom


May I save the call init(&m_sbuf); in basic_onullstre am's ctor as this
same call is made in basic_ostream's ctor?


My preference would be to keep init and change the initialiser list

basic_onullstre am()
{
init(&m_sbuf);
}

john
Nov 24 '05 #8

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:yN******** *******@newsfe1-gui.ntli.net...
Angel Tsankov wrote:

"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:

How do I define a null ostream that inherits publicly
std::ostream and
ignores anything that would otherwise be output?
I cribbed this from c.l.c++ years ago - don't remember the
original
author (but it's not me):

#include <streambuf>
#include <ostream>

template <class cT, class traits = std::char_trait s<cT> >
class basic_nullbuf: public std::basic_stre ambuf<cT, traits> {
typename traits::int_typ e overflow(typena me traits::int_typ e c)
{
return traits::not_eof (c); // indicate success
}
};

template <class cT, class traits = std::char_trait s<cT> >
class basic_onullstre am: public std::basic_ostr eam<cT, traits> {
public:
basic_onullstre am():
std::basic_ios< cT, traits>(&m_sbuf ),
std::basic_ostr eam<cT, traits>(&m_sbuf )
{
init(&m_sbuf);
}

private:
basic_nullbuf<c T, traits> m_sbuf;
};

typedef basic_onullstre am<char> onullstream;
typedef basic_onullstre am<wchar_t> wonullstream;

Best regards,

Tom


May I save the call init(&m_sbuf); in basic_onullstre am's ctor as
this same call is made in basic_ostream's ctor?


My preference would be to keep init and change the initialiser list

basic_onullstre am()
{
init(&m_sbuf);
}

john


Well, I'm not sure this is a good idea, as ctors of virtual bases must
be called by any derived class ctor, right?

Nov 24 '05 #9

Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?


You can just set the failbit on any ostream. This makes it effectively
ignore all output sent to it until you unset the bit.

Nov 24 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8249
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it at all possible?
7
14364
by: Stewart Gordon | last post by:
Using g++ 3.1, Mac OS X. I'm doing something trying to involve a null output stream, i.e. a stream that just discards what goes into it. The idea is that it gives a simple way of producing debugging output, which can be controlled at runtime. For example: Debug(5) << "Doing something..." << endl;
13
10805
by: Bo Peng | last post by:
Dear list, I need to add "disable output" feature to a bunch of objects that output to ostream (or ofstream). The least intrusive way to do this is pass 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? Thanks. Bo
6
3093
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int argc, char *argv ) { char *q = 0; std::cout << q << "\n";
23
7368
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion....
4
2190
by: isanbard | last post by:
Hi all, I'd like to do something like this: #include <ostream> std::ostream cnull(0); #ifdef NDEBUG #define DOUT cnull
4
10632
by: toton | last post by:
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...
6
4710
by: syang8 | last post by:
Any one can specify the problem of the following code? The compiling error is on the friend function. If the base class is not inherited from ostream, or I just remove the friend function from the base class, the code will compile. ------------------------------------------------------------------------- #include <iostream> ...
11
3616
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I tried a std::ostringstream: std::ostringstream oss; oss << x << y << z; std::string result ( oss.str() ); The result shows that feeding the...
0
7336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7675
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5877
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5257
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4884
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3384
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3385
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
948
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.