Connecting Tech Pros Worldwide Forums | Help | Site Map

restoring the default format state for an ostream

Matt
Guest
 
Posts: n/a
#1: May 2 '06
Does the standard define default values for the format state of an
ostream? I would like to be able to put a stream in a standard format
state without thinking about how some other routine may have changed the
state.

I don't see the answer in Stroustrup or in the parashift FAQs.

I would like to write a statement like

my_ostream.restore_format_state_defaults();

Is there anything like that in ios_base or elsewhere?

I see copyfmt(), but Stroustrup doesn't say much about what it is
supposed to do. It's not clear how I could (say) copy a format to a
global variable at startup and restore from that copy.
mlimber
Guest
 
Posts: n/a
#2: May 2 '06

re: restoring the default format state for an ostream


Matt wrote:[color=blue]
> Does the standard define default values for the format state of an
> ostream? I would like to be able to put a stream in a standard format
> state without thinking about how some other routine may have changed the
> state.
>
> I don't see the answer in Stroustrup or in the parashift FAQs.
>
> I would like to write a statement like
>
> my_ostream.restore_format_state_defaults();
>
> Is there anything like that in ios_base or elsewhere?
>
> I see copyfmt(), but Stroustrup doesn't say much about what it is
> supposed to do. It's not clear how I could (say) copy a format to a
> global variable at startup and restore from that copy.[/color]

Perhaps what you need is just to use your own stream (a la Stroustrup,
TC++PL 3rd ed., sec. 21.4.6.3):

struct MyT { std::string& Stringify() const; /*...*/ };

std::ostream& operator<<( std::ostream& os, const MyT& myObj )
{
std::ostringstream oss; // In default state
oss << myObj.Stringify();
return os << oss.str();
}

The existing state could still affect your result, but this technique
eliminates many potential issues such as precision, base, etc.

Cheers! --M

Marcus Kwok
Guest
 
Posts: n/a
#3: May 2 '06

re: restoring the default format state for an ostream


Matt <themattfella@xxyyyzzzz.com> wrote:[color=blue]
> Does the standard define default values for the format state of an
> ostream? I would like to be able to put a stream in a standard format
> state without thinking about how some other routine may have changed the
> state.
>
> I don't see the answer in Stroustrup or in the parashift FAQs.
>
> I would like to write a statement like
>
> my_ostream.restore_format_state_defaults();
>
> Is there anything like that in ios_base or elsewhere?
>
> I see copyfmt(), but Stroustrup doesn't say much about what it is
> supposed to do. It's not clear how I could (say) copy a format to a
> global variable at startup and restore from that copy.[/color]

I had a similar question about this:
http://groups.google.com/group/comp....dfcd0b2d5f742/

Instead of globally saving and restoring, basically I save and restore
the format during the output statements. You may be able to modify this
to do what you want.

Here is a little test program demonstrating the technique:


#include <iostream>
#include <iomanip>

struct Data {
double d;
};

std::ostream&
operator<<(std::ostream& o, const Data& d)
{
std::ios_base::fmtflags flags = o.setf(std::ios_base::fixed);
std::streamsize precision = o.precision(3);
o << "during: " << d.d;
o.precision(precision);
o.flags(flags);
return o;
}

int main()
{
Data d;
d.d = 3.14159;

double dd;
dd = 3.14159;
std::cout << "before: " << dd << '\n';
std::cout << d << '\n';
std::cout << " after: " << dd << '\n';

return 0;
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Matt
Guest
 
Posts: n/a
#4: May 5 '06

re: restoring the default format state for an ostream


Matt wrote:[color=blue]
> Does the standard define default values for the format state of an
> ostream? I would like to be able to put a stream in a standard format
> state without thinking about how some other routine may have changed the
> state.
>
> I don't see the answer in Stroustrup or in the parashift FAQs.
>
> I would like to write a statement like
>
> my_ostream.restore_format_state_defaults();
>
> Is there anything like that in ios_base or elsewhere?
>
> I see copyfmt(), but Stroustrup doesn't say much about what it is
> supposed to do. It's not clear how I could (say) copy a format to a
> global variable at startup and restore from that copy.[/color]

Thanks for the replies.

After looking at it some more, the following became clear:

#include <iostream>
#include <sstream>
#include <iomanip>

const ostringstream standard_format;

void put(ostream& os) {
os << showpoint << setprecision
// ... other operations that change the format state
// ... ouputs of objects
<< endl
;

os.copyfmt(standard_format); // restores os to standard state
}
Closed Thread