473,659 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.rest ore_format_stat e_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 5429
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.rest ore_format_stat e_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::ostringstr eam 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**********@x xyyyzzzz.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.rest ore_format_stat e_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(pre cision);
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.rest ore_format_stat e_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(stan dard_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
5447
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 module to implement this. Capturing current program state and neatly replacing it later is proving to be trickier than I first imagined, so I'm here to ask for a little direction from wiser minds than mine! When my program initializes, each game...
2
7751
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 level function which accepts a "generic C++ stream" as an argument (cerr, cout, ofstream, etc). I have tried to make the parameter a const ostream&, but when I plug in cerr or cout I get a compiler error. What am I doing wrong? // The low...
3
5288
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 ON GO in a .sql file, but would like to reset them to their previous settings
2
3072
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
5843
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 argument of function must be a constant value. Here is my sample code, it's compiled successully in VC7.1. #include<iostream> class Base{ public :
15
3734
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: #include <iostream> #include <iomanip> struct Data { double d; };
4
1580
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 the clean version? Q2: Is there a way to do what I want to do "cleanly" (as I define it :) The templated function writes a long to a binary stream in 64 bits. This code works for 32 and 64 bit long.
3
3327
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 ocucrring on the line "ostream myfile1;" Does any one know about this error? how to make it go away?. My requirement is to write some do debug logging into a file. But I am getting this error.
5
6552
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. The SAS users are laughing because they can (to a certain extend) easily go back to an earlier SAS table by simply starting dsm. Of course, a flat file table is different than a relational table; but still, the trouble related to restoring DB2...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.