Predictably formated output with std::cout?

Discussion in 'C++' started by Carsten Fuchs, Jul 7, 2008.

  1. 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
    Carsten Fuchs, Jul 7, 2008
    #1
    1. Advertising

  2. Carsten Fuchs

    Stefan Ram Guest

    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::eek: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.
    Stefan Ram, Jul 7, 2008
    #2
    1. Advertising

  3. 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.
    Gennaro Prota, Jul 7, 2008
    #3
  4. Carsten Fuchs

    Guest

    On Jul 7, 9:56 am, Carsten Fuchs <> 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
    , Jul 7, 2008
    #4
  5. Carsten Fuchs

    Guest

    On Jul 7, 6:21 pm, -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::eek:stream out(std::cout.rdbuf());

    out << "state free formatted output";

    Kind regards,
    Vladimir
    , Jul 8, 2008
    #5
  6. 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::eek: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
    Carsten Fuchs, Jul 8, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,405
    Leor Zolman
    Jun 2, 2004
  2. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    479
  3. walterbyrd
    Replies:
    13
    Views:
    1,296
    walterbyrd
    May 13, 2009
  4. saurabh29789

    Sending std::cout to std::cout !!

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    1
    Views:
    290
    Harald Finster
    Jun 11, 2009
  5. saurabh29789

    Sending std::cout to std::cout

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    2
    Views:
    342
    Rolf Magnus
    Jun 12, 2009
Loading...

Share This Page