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 6 1583
"William Payne" <mi************ **@student.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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:DLo5d.2621 79$Fg5.185548@a ttbi_s53... "William Payne" <mi************ **@student.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
William Payne wrote: 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)
Victor Bazarov wrote: 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
"William Payne" <mi************ **@student.liu. se> wrote in message
news:cj******** **@news.island. liu.se... "Victor Bazarov" <v.********@com Acast.net> wrote in message news:DLo5d.2621 79$Fg5.185548@a ttbi_s53... "William Payne" <mi************ **@student.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
"Victor Bazarov" <v.********@com Acast.net> wrote in message news:DLo5d.2621 79$Fg5.185548@a ttbi_s53... 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Trevor |
last post by:
Hello,
Please bear with me, I am trying to learn C++. I am implementing some
error/debug functions which format a message and output it to a C++ stream.
I would like to design it using one low level function which accepts a
"generic C++ stream" as an argument (cerr, cout, ofstream, etc). I have
tried to make the parameter a const ostream&, but when I plug in cerr or
cout I get a compiler error. What am I doing wrong?
// The low...
|
by: Matt Garman |
last post by:
I'd like to create a "custom output facility". In other words, I want
an object whose use is similar to std::cout/std::cerr, but offers more
flexibility. Instead of simply writing the parameter to stdout/stderr,
I'd like it to write to stdout, to a file, and/or call a logging
function.
So my output function might look something like this:
OutputFacility& OutputFacility::put(const std::string& s) {
cout << s; // print to stdout
|
by: Jason |
last post by:
Hello,
I have a class, transCore, that does certain work, and by default, prints
its progress to the stand output.
Later, I will to write a GUI class that encapsulate the transCore class. I
would like to redirect/intercept the output of transCore class to this GUI
class. I wrote a sample program that demonstrate how I did it, but I am not
sure if it's any good. Can anyone critic my code? If there's any
non-standard conforming code or...
|
by: radnoraj |
last post by:
Hi,
I am sucessfull in redirecting console output to a file. but in this
case nothing is displayed on the console, cout output is written to
file without display.
how do write the output to console as well as to file, my code is as
below,
=======================================================================
#include <iostream.h>
#include<ostream>
#include<sstream>
|
by: FS Liu |
last post by:
Hi,
I am writing ATL Service application (XML Web service) in
VS.NET C++.
Are there any sample programs that accept XML as input and
XML as output
in the web service?
Thank you very much.
| |
by: ray.ackley |
last post by:
Hello all,
I have an app that uses a BackgroundWorker() object to read USB data
that is being fed in serially (FT245R chip, www.ftdichip.com). It
looks just like a serial data stream. Anyhow, it reads in the bytes
and finds the header and footer and pulls packets out of the data
stream. Nice, easy stuff. It then stuffs the packet into into a
class I made up to pull the data out through the ProgressChanged
event. The class is pretty...
|
by: deepak |
last post by:
How the following code is working.
main() {
int a = 38, b = 13;
unsigned long long c;
c = a * (1<<b) * 32000;
printf("%llu", c);
|
by: mathieu |
last post by:
Hi there,
I am trying to rewrite this very slow function (due to AFAIK the
extra copy):
void DoAction(std::istream &is, std::ostream &os)
{
uint16_t c;
while( is.read((char*)&c,2) )
|
by: CarlosMB |
last post by:
Hello,
I am writing code that uses a DLL which is supposed to print to
console
some useful information but for some reason it is not doing so. The
environment
is a bit complex to explain but here it goes:
- I am using a C library called SYMPHONY, which I compiled myself.
When using that
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |