 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
William Payne Guest
|
Posted: Sat Sep 25, 2004 10:42 pm Post subject: Perform output if stream is good |
|
|
Hi, I have a function declared as:
void foo(const std::string& s, std::ostream& verbose_output);
I want foo() to write a lot of data to the ostream if it's a valid stream.
If it's valid or not should depend on user input (command line arguments
actually). If the user decides he/she wants verbose output, I will pass
std::cout as the last argument when calling foo(). But what should I pass if
the user doesn't want any output? And how should foo() check if the stream
is good? verbose_output.good()?.
Thanks for any replies
/ WP
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Sep 26, 2004 1:16 am Post subject: Re: Perform output if stream is good |
|
|
"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote...
| Quote: | void foo(const std::string& s, std::ostream& verbose_output);
I want foo() to write a lot of data to the ostream if it's a valid stream.
If it's valid or not should depend on user input (command line arguments
actually). If the user decides he/she wants verbose output, I will pass
std::cout as the last argument when calling foo(). But what should I pass
if the user doesn't want any output? And how should foo() check if the
stream is good? verbose_output.good()?.
|
Yes...
Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.
The problem is that if you have a reference to a stream, it must be some
real stream, otherwise where did you get a reference to it. Of course,
you could use 'ostringstream' instead, but I am not sure what you make it
"bad"...
Victor
|
|
| Back to top |
|
 |
William Payne Guest
|
Posted: Sun Sep 26, 2004 8:19 am Post subject: Re: Perform output if stream is good |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | "William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote...
void foo(const std::string& s, std::ostream& verbose_output);
I want foo() to write a lot of data to the ostream if it's a valid
stream. If it's valid or not should depend on user input (command line
arguments actually). If the user decides he/she wants verbose output, I
will pass std::cout as the last argument when calling foo(). But what
should I pass if the user doesn't want any output? And how should foo()
check if the stream is good? verbose_output.good()?.
Yes...
Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.
The problem is that if you have a reference to a stream, it must be some
real stream, otherwise where did you get a reference to it. Of course,
you could use 'ostringstream' instead, but I am not sure what you make it
"bad"...
Victor
|
Thanks for your reply, Victor. I think I will just add a third parameter to
foo(), a boolean telling it if to use the stream or not.
/ WP
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sun Sep 26, 2004 2:45 pm Post subject: Re: Perform output if stream is good |
|
|
William Payne wrote:
| Quote: |
Hi, I have a function declared as:
void foo(const std::string& s, std::ostream& verbose_output);
I want foo() to write a lot of data to the ostream if it's a valid stream.
|
If the stream isn't valid writes to it do nothing. So all you need to do
is set badbit.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
|
|
| Back to top |
|
 |
Julián Albo Guest
|
Posted: Sun Sep 26, 2004 5:54 pm Post subject: Re: Perform output if stream is good |
|
|
Victor Bazarov wrote:
| Quote: | I want foo() to write a lot of data to the ostream if it's a valid
stream. If it's valid or not should depend on user input (command line
arguments actually). If the user decides he/she wants verbose output, I
will pass std::cout as the last argument when calling foo(). But what
should I pass if the user doesn't want any output? And how should foo()
check if the stream is good? verbose_output.good()?.
Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.
|
I have this tiny class that can be useful:
//*********************************************************
// Null ostream class.
//*********************************************************
class Nullbuff : public std::streambuf
{
public:
Nullbuff ()
{
setbuf (0, 0);
}
protected:
int overflow (int)
{
setp (pbase (), epptr () );
return 0;
}
int sync ()
{
return 0;
}
};
class Nullostream : public std::ostream
{
public:
Nullostream () :
std::ostream (& buff)
{ }
private:
Nullbuff buff;
};
--
Salu2
|
|
| Back to top |
|
 |
David Hilsee Guest
|
Posted: Sun Sep 26, 2004 8:45 pm Post subject: Re: Perform output if stream is good |
|
|
"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote
| Quote: |
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:DLo5d.262179$Fg5.185548 (AT) attbi_s53 (DOT) ..
"William Payne" <mikas493_no_spam (AT) student (DOT) liu.se> wrote...
void foo(const std::string& s, std::ostream& verbose_output);
I want foo() to write a lot of data to the ostream if it's a valid
stream. If it's valid or not should depend on user input (command line
arguments actually). If the user decides he/she wants verbose output, I
will pass std::cout as the last argument when calling foo(). But what
should I pass if the user doesn't want any output? And how should foo()
check if the stream is good? verbose_output.good()?.
Yes...
Invent your own stream with '/dev/null' buffer which will always be
valid
and pass when you don't need verbosity.
The problem is that if you have a reference to a stream, it must be some
real stream, otherwise where did you get a reference to it. Of course,
you could use 'ostringstream' instead, but I am not sure what you make
it
"bad"...
Victor
Thanks for your reply, Victor. I think I will just add a third parameter
to
foo(), a boolean telling it if to use the stream or not.
|
Pointers are typically used for "optional" parameters. If you use a pointer
instead of a reference, you can pass a null pointer when you don't want the
output, and check to see if the pointer is null instead of using a separate
boolean value. I think most programmers would find that approach more
appealing. I'm assuming that foo() will completely ignore the stream when
the boolean flag indicates that it should, of course.
--
David Hilsee
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Mon Sep 27, 2004 5:46 pm Post subject: Re: Perform output if stream is good |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: |
Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.
|
You can just write your own streambuf class that just has no-op put functions.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|