Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Predictably formated output with std::cout?

Reply
Thread Tools

Predictably formated output with std::cout?

 
 
Carsten Fuchs
Guest
Posts: n/a
 
      07-07-2008
Dear group,

I was wondering what the best way is to produce predictable output with std::cout (and other output
streams)?

That is, one of the biggest weaknesses of std::cout seems to be the fact that a simple statement like

int i=1234;
cout << i;

can yield different outputs depending on the flags set earlier (e.g. decimal, hex or oct numbers).

Due to the global nature of std::cout, any component (e.g. a third-party library or module) of a
program that sets any ios_base flags for its purposes may make the output of std::cout for the rest
of the program entirely unpredictable!

Well, I'm looking for the "best" method in order to solve this unpredictability, or at least for the
most common solutions to the problem.

It's certainly possible to reset or properly set all the flags prior to each cout output manually,
but in practice, that's infeasible (e.g. when one third-party library sets certain flags, and the
other doesn't have the "reset" code) and cumbersome (the flags setup code would be a lot longer than
the actual cout << i; statement).

(Note that good old printf(), despite its many problems and disadvantages, used to be entirely free
of this kind of problem due to its stateless nature. printf("%i", i); though not type-safe and
not useful for custom classes, is short and predictable...)

I'd be very happy about your advice!

Thank you very much in advance, and
best regards,
Carsten
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-07-2008
Carsten Fuchs <> writes:
>(Note that good old printf(), despite its many problems and
>disadvantages, used to be entirely free of this kind of problem
>due to its stateless nature. printf("%i", i); though not
>type-safe and not useful for custom classes, is short and
>predictable...)


Possibly, you can create a new ostream object associated with
stdout and then use this with custom settings instead of cout.
I don't know the details. Maybe something like »new ostream
( new filebuf( stdout, ios_base:ut ))« or so. Possibly, some
care is needed to synchronize this with the standard streams.

Or, you could use a stringstream with its own settings for
formatting and then write the resulting string to ::std::cout
as an unmodified text.

 
Reply With Quote
 
 
 
 
Gennaro Prota
Guest
Posts: n/a
 
      07-07-2008
Carsten Fuchs wrote:
> [...]
> It's certainly possible to reset or properly set all the flags prior to
> each cout output manually, but in practice, that's infeasible (e.g. when
> one third-party library sets certain flags, and the other doesn't have
> the "reset" code) and cumbersome (the flags setup code would be a lot
> longer than the actual cout << i; statement).


You might want to look at the I/O Stream-State Saver Library, in Boost.

Personally, I find that they went for a too fine-grained set of
classes and have my own saver, but your mileage may vary. Differently
from Boost, anyway, I have on my side that James Kanze also uses a
single saver class in the code available at his site (Seriously,
it's likely that James' code was written way before Boost had a State
Saver library, but still I make a point that his solution is a winner
in terms of simplicity and maintenance cost)

--
Gennaro Prota | <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
 
Reply With Quote
 
acehreli@gmail.com
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 9:56*am, Carsten Fuchs <CarstenFu...@T-Online.de> wrote:
> Dear group,
>
> I was wondering what the best way is to produce predictable output with std::cout (and other output
> streams)?
>
> That is, one of the biggest weaknesses of std::cout seems to be the fact that a simple statement like
>
> int i=1234;
> cout << i;


One option is to leave cout to the others and maintain your own
ostream which shares the same output buffer (but not formatting) with
cout:

#include <iostream>
#include <iomanip>

using namespace std;

void test(const char * label, ostream & os)
{
os << label << ": " << 42 << ' ' << 100 << '\n';
}

int main()
{
ostream mycout(cout.rdbuf());

cout << hex;

test("cout ", cout);
test("mycout", mycout); // not effected by cout's state
}

The output:

cout : 2a 64
mycout: 42 100

Ali
 
Reply With Quote
 
vova777@gmail.com
Guest
Posts: n/a
 
      07-08-2008
On Jul 7, 6:21*pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
> * Possibly, you can create a new ostream object associated with
> * stdout and then use this with custom settings instead of cout.


That is as simple as -

std:stream out(std::cout.rdbuf());

out << "state free formatted output";

Kind regards,
Vladimir
 
Reply With Quote
 
Carsten Fuchs
Guest
Posts: n/a
 
      07-08-2008
Hi all,

thank you very much to everyone for your answers, they helped a lot!

The boost stream-state saver library sounds good, but I guess that the own ostream instance as
described by Vladimir and Ali, that is:

wrote:
> std:stream out(std::cout.rdbuf());
> out << "state free formatted output";


is what I was looking for and fits my case best.

Many thanks for your help!

Best regards,
Carsten
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT: URL with a predictably long load time? Terry Pinnell HTML 14 03-30-2011 11:50 AM
Predictably Breaking Up a Cartesian Product Brad C++ 9 07-15-2010 08:52 PM
Convert UNIX formated text files to DOS formated? walterbyrd Python 13 05-13-2009 02:32 PM
output formated text with xalan? Elhanan XML 5 05-07-2006 02:32 PM
Is Your Software Working A Little Too Predictably? thoran@thoran.com Ruby 4 03-19-2006 08:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57