Home arrow C++ arrow General Stream Manipulation in C++

General Stream Manipulation in C++

Using streams in C++ is an easy and intuitive task. However, sometimes you may want to customize the input/output on a deeper level. This is possible in a powerful and easy way with the use of the manipulators.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
April 28, 2009
  1. · General Stream Manipulation in C++
  2. · Numeric Systems and Bool Values
  3. · Float Formatting
  4. · Closing Thoughts

print this article

General Stream Manipulation in C++
(Page 1 of 4 )

If you've programmed in C already and used the “scanf” function, you already know what I am talking about here. Writing a float value on the screen with two digits of precision after the decimal point is done on the fly. A strong library such as C++ must have something similar implemented.

Once you've gone through this article, you will see that even more. You can go as far as to create your own formatters/manipulators, but that will be the subject of a different article, so stay tuned for that one.

For now let’s see what we have implemented. Manipulators are functions that are applied with the insertion operator << in the case of output streams. They are declared in the <iostream>, <ios> or the <iomanip> header, depending on the area they are covering.

Eventually manipulators modify a couple of flags that define the behavior of the stream, defined all the way up in the ios_base class. Nevertheless, before we apply any of them, we should first grasp the knowledge of how we can reset all that we have modified.

The “flags” member function of each stream is overloaded in two ways. First, one takes no parameters and returns the current format state. The return type is a defined type inside the stream class, namely “fmtflags.” I will present my examples on the cin and cout stream connected to the console window, but they will work on any stream class. Each time you want to save it, just act as follows:

#include <iostream>

using namespace std;

int main ()


ostream::fmtflags basicFlag = cout.flags();

// Do manipulations on the stream // (1)

ostream::fmtflags usedFlag = cout.flags(basicFlag);


// Apply new formatting flags

cout.flags(usedFlag); // recall the format used at (1)


// Print what you want with the first collection

cout.flags(basicFlag); // reset the console stream flags

return 0;


This first example I’ve extended a little so you can see the header I use, and that I also added the std namespace to the global namespace, as I’m a bit too lazy to write the “std::” keyword in front of each function used.

Now we have saved in the “basicFlag” variable, in matter of effect, the current format setting applied to the cout stream. The second overload of the flags function will handle the set process. It will also return the current collection of flag settings inside the stream and set the new collection that you gave as input.

As you may expect, you do not necessarily need to save the usedFlag before the change. You could also just call it as we did in the end. We set the new settings, in fact the basic settings, saved at the start.

As far as the library where you’ll find them, most of the manipulators are found inside the iostream header; however, the ones taking an argument also will have their place in the “iomanip” header, so make sure to include that as well, once you use one of them on your stream.

All of the flag changes can be resolved with the rdstate() and setstate() member functions of streams, but this is harder to read and manage. Manipulators provide a more elegant solution, so I recommend using them; however, be aware that when using these, the changes are permanent (except one that confirms the rule :P, but we will talk about that also).

If you set a flag, it will stay until you explicitly modify it. The cin/cout streams are a prime example of this, as most programmers would like to have them working in a default mode. So in those situations where you use modifiers, make sure you delimit the modifications with a state save and a restore.

If you thought that there were already too many ways available to modify them, let me say to you that the flag sets can be done with the setf() function also, and unsetf() will turn off a flag. As in the example below; do not think about what it does, just observe the method:

cout.setf( ios_base::dec );

cout.unsetf( ios_base::dec );

In fact, the “setf” has an overload that takes two parameters, where the second is the flags that are to be turned off. This trick can save you a couple of lines when you want to turn some flags on/off at the same time.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials