Home arrow C++ arrow Page 4 - 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 - TableFormatter Template
(Page 4 of 4 )

As with all other character-based classes in the standard library, manipulators work on streams that use narrow or wide characters. Therefore, you can use them with templates to write formatting utilities that operate on streams of any kind of character. Example 10-2 presents the class template TableFormatter, which formats data into equal-width columns and writes it to a stream.

Example 10-2. A generic class for tabular data

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

// TableFormatter formats data for output to a stream of characters
// of type T.
template<typename T>
class TableFormatter {

   TableFormatter(basic_ostream<T>& os) : out_(os) {}
  ~TableFormatter() {out_ << flush;}

   template<typename valT>
   void writeTableRow(const vector<valT>& v, int width);

  basic_ostream<T>& out_;

template<typename T>     // refers to class template param list
template<typename valT>  // refers to mem fn template param list
void TableFormatter<T>::writeTableRow(const std::vector<valT>& v,
int width) {

  ios_base::fmtflags flags = out_.flags();

  out_ << setprecision(2) << fixed;  // Set the precision, in case
                                     // this is floating-point data
  for (vector<valT>::const_iterator p = v.begin();
       p != v.end(); ++p)
    out_ << setw(width) << left << *p;  // Set the width, justify,
// and write the element
  out_ << endl;     // Flush
  out_.setf(flags); // Set the flags back to normal

int main() {

   TableFormatter<char> fmt(cout);

   vector<string> vs;

   vs.push_back( "Sunday" );
   vs.push_back( "Monday" );
   vs.push_back( "Tuesday" );

   fmt.writeTableRow(vs, 12);
   fmt.writeTableRow(vs, 12);
   fmt.writeTableRow(vs, 12);

   vector<double> vd;


   fmt.writeTableRow(vd, 5);

The output from Example 10-2 looks like this:

  Sunday     Monday     Tuesday
  4.00 3.00 2.00 1.00

See Also

Table 10-1, Recipe 10.2

Please check back next week for the continuation of this article.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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