restoring the default format state for an ostream

M

Matt

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.
 
M

mlimber

Matt said:
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.

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::eek:stream& operator<<( std::eek:stream& os, const MyT& myObj )
{
std::eek:stringstream 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
 
M

Marcus Kwok

Matt said:
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.lang.c++/browse_frm/thread/296dfcd0b2d5f742/

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::eek:stream&
operator<<(std::eek:stream& 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;
}
 
M

Matt

Matt said:
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.

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
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top