Matt <th**********@xxyyyzzzz.com> wrote:
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.
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