Prev: pros and cons of returning const ref to string instead of string by value
Next: pros and cons of returning const ref to string instead of string by value
From: Alona on 3 Dec 2009 01:31 Hi All, Assume I put the following in the code: cout << setw(pos) << setfill('*') << " "; cout << setiosflags(ios::left) << setw(len) << s << " : " << str << endl; How can I return things back to 'normal'? How should I unset width, left alignment, padding character, etc? Thank you, Alona -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Frank Birbacher on 6 Dec 2009 03:23 Hi! Alona schrieb: > Assume I put the following in the code: > > cout << setw(pos) << setfill('*') << " "; > cout << setiosflags(ios::left) << setw(len) << s << " : " << str << > endl; > > How can I return things back to 'normal'? How should I unset width, > left alignment, padding character, etc? > You have to get the information and store it before you change the settings. For example: const char originalFill = cout.fill(); cout << setw(pos) << setfill("*") << " "; cout.fill(originalFill); //or cout << setfill(originalFill); There are some member functions of cout which give access to the format flags, precision, and so on. You can see a reference here: http://www.cplusplus.com/reference/iostream/ostream/ This is tedious and a little error prone. At least it is lots of code. But you can use what is called a "state saver". See here for information on that concept: http://www.boost.org/doc/libs/1_40_0/libs/io/doc/ios_state.html Frank -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Andrew on 7 Dec 2009 02:53 On 6 Dec, 20:23, Frank Birbacher <bloodymir.c...(a)gmx.net> wrote: > > Assume I put the following in the code: > > > cout << setw(pos) << setfill('*') << " "; > > cout << setiosflags(ios::left) << setw(len) << s << " : " << str << > > endl; > > > How can I return things back to 'normal'? I don't know. And I think it's a right pain that you have to (but, unfortunately, you do). So I get around the problem by NEVER altering the standard output streams in this way. I always use a std::stringstream to format what I want and then output the formatted result to std::cout. My way is more expensive since I am creating and destroying stringstreams, but it does get around that problem. Regards, Andrew Marlow -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Alona on 7 Dec 2009 18:15 On Dec 7, 2:53 pm, Andrew <marlow.and...(a)googlemail.com> wrote: > On 6 Dec, 20:23, Frank Birbacher <bloodymir.c...(a)gmx.net> wrote: > > > > Assume I put the following in the code: > > > > cout << setw(pos) << setfill('*') << " "; > > > cout << setiosflags(ios::left) << setw(len) << s << " : " << str << > > > endl; > > > > How can I return things back to 'normal'? > > I don't know. And I think it's a right pain that you have to (but, > unfortunately, you do). So I get around the problem by NEVER altering > the standard output streams in this way. I always use a > std::stringstream to format what I want and then output the formatted > result to std::cout. My way is more expensive since I am creating and > destroying stringstreams, but it does get around that problem. > > Regards, > > Andrew Marlow > > -- > [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ] > [ comp.lang.c++.moderated. First time posters: Do this! ] { Please remove the banner from your quoting. -mod } I figured it was a call to resetiosflags(). Has anyone experienced any problems with it? setw() is valotile and should not be reset after a single write. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 8 Dec 2009 07:32
Andrew wrote: > On 6 Dec, 20:23, Frank Birbacher <bloodymir.c...(a)gmx.net> wrote: >>> Assume I put the following in the code: >>> cout << setw(pos) << setfill('*') << " "; >>> cout << setiosflags(ios::left) << setw(len) << s << " : " << str << >>> endl; >>> How can I return things back to 'normal'? > > I don't know. And I think it's a right pain that you have to (but, > unfortunately, you do). And I feel it was a design mistake. My experience is that persistent formatting states are almost never useful. In almost all of the serious programs that I have written, I had to either use a separate stream (e.g. through boost::lexical_cast) or save and restore the states. Both of which are a pain and a nuisance. To elaborate this a little bit, formatting is usually per-field: for example, you format the latitude and the longitude in a certain way, the altitude in another way, the signal strength in another way, and so on. (Especially because they have different units -- which means they belong to different number spaces -- and different precisions.) And it's not very common that you print a bunch of latitudes, and then a bunch of signal strengths, etc., in which case persistent formatting states would help, but instead you usually print them in an interleaved manner, in which case persistent formatting states come as a burden. Hmm, on the other hand, we don't even have a way to align numbers at the decimal points, so we could say the default iostream formatting facility is inadequate for serious purposes anyway... ;-) -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |