473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

restoring the default format state for an ostream

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.
May 2 '06 #1
3 5367
Matt 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.


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

May 2 '06 #2
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
May 2 '06 #3
Matt 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.


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
}
May 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob H | last post by:
Hello list... I'm developing an adventure game in Python (which of course is lots of fun). One of the features is the ability to save games and restore the saves later. I'm using the pickle...
2
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low...
3
by: Florian | last post by:
I need to set multiple values for some SQL statements, for example SET NUMERIC_ROUNDABORT OFF GO SET ANSI_PADDING,ANSI_WARNINGS,CONCAT_NULL_YIELDS_NULL,ARITHABORT,QUOTED_IDENTIF IER,ANSI_NULLS...
2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
4
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
15
by: Marcus Kwok | last post by:
How do you save the formatting options for a stream? In the program below, notice that when I define my operator<<() for the custom type, it permanently changes the output format of the stream: ...
4
by: chrisstankevitz | last post by:
Apparently default template arguments can only be used in classes. I include two apps below. One works but is IMO messy. The other does not work and is IMO clean. Q1: Why would c++ dissallow...
3
by: Sabiyur | last post by:
Hi All, While compiling the dll, I am getting the below error. "error C2512: 'basic_ostream<char,struct std::char_traits<char' : no appropriate default constructor available" The error is...
5
by: Troels Arvin | last post by:
Hello, Every so often, I'm asked to help people recover data from tables that were either dropped or where to much data was DELETEed. The complications related to restoring data are a problem....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.