Home arrow C++ arrow Page 3 - Streams and Files

Streams and Files

Over the course of this five-part series, you will learn how to handle stream output in C++, how to operate on files, and more. This article is excerpted from chapter 10 of the C++ Cookbook, written by Ryan Stephens, Christopher Diggins, Jonathan Turkanis and Jeff Cogswell (O'Reilly; ISBN: 0596007612). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
October 16, 2008
  1. · Streams and Files
  2. · 10.1 Lining Up Text Output Problem
  3. · Tables for Text Manipulation
  4. · TableFormatter Template

print this article

Streams and Files - Tables for Text Manipulation
(Page 3 of 4 )

Table 10-1 contains a summary of manipulators that operate on any kind of value (text, float, integer, etc.). There is a set of manipulators that apply only to floating-point output, and they are described in Recipe 10.2.

Table 10-1. Text manipulators

Manipulator Description Sample output

Justify values within the current field width to either the left or right side, and pad the remaining space with the fill character.


apple bananna cherry

right  Right-justified (with a field width of 10 ):
setw(int n) Set the width of the field to n characters wide. See earlier example.

Table 10-1. Text manipulations (continued) 



Sample output

setfill(int c)

Use the character cto pad fields that have remaining space.

cout << setfill('.') << setw(10) << right << "foo"







boolalpha noboolalpha

Display Boolean values as the current locale’s representa-tion of the words trueand false, instead of 1and 0.

cout << boolalpha << true








Write a newlineto the stream and flush the output buffer.



Write a nullcharacter (‘\0’) to the stream.



Flush the output buffer.


Some of the manipulators in Table 10-1 (and Table 10-2 in the next recipe) toggle binary stream flags, and are actually implemented as two manipulators that turn a flag on or off. Take boolalpha, for example. If you want Boolean values to be displayed as their written equivalents in the current locale (e.g., "true" and "false"), use the boolalpha manipulator. To turn this behavior off, so that 0 and 1 are printed instead, use noboolalpha, which is the default.

All manipulators have the behavior that they stay in effect until they are explicitly changed, except for setw. In Example 10-1, you can see that it is called before each write, but left is used only once. This is because the width is reset to zero after each value is written to the stream with operator<<; to keep the same width for each field, I had to call setw each time.

The standard manipulators provide a lot of functionality, but they don't do everything. If you want to write your own manipulators, see Recipe 10.2.

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