C++
  Home arrow C++ arrow Manipulating Streams and Files with C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Manipulating Streams and Files with C++
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-10-23

    Table of Contents:
  • Manipulating Streams and Files with C++
  • 10.3 Writing Your Own Stream Manipulators
  • 10.4 Making a Class Writable to a Stream
  • 10.5 Making a Class Readable from a Stream

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Manipulating Streams and Files with C++


    (Page 1 of 4 )

    In this second part of a five-part series on handling streams and files in C++, you will learn how to format floating-point output, how to write your own stream manipulators, 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.

    10.2 Formatting Floating-Point Output

    Problem

    You need to present floating-point output in a well-defined format, either for the sake of precision (scientific versus fixed-point notation) or simply to line up decimal points vertically for easier reading.

    Solution

    Use the standard manipulators provided in <iomanip> and <ios> to control the format of floating-point values that are written to the stream. There are too many combinations of ways to cover here, but Example 10-3 offers a few different ways to display the value of pi.

    Example 10-3. Formatting pi

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

    using namespace std;

    int main() {

       ios_base::fmtflags flags =  // Save old flags
          cout.flags();

       double pi = 3.14285714;

       cout << "pi = " << setprecision(5)  // Normal (default) mode; only
            << pi << '\n';                 // show 5 digits, including both
                                           // sides of decimal point.

       cout << "pi = " << fixed            // Fixed-point mode;
            << showpos                     // show a "+" for positive nums,
            << setprecision(3)             // show 3 digits to the *right*
            << pi << '\n';                 // of the decimal.

       cout << "pi = " << scientific       // Scientific mode;
            << noshowpos                   // don't show plus sign anymore
            << pi * 1000 << '\n';

       cout.flags(flags);  // Set the flags to the way they were
    }

    This will produce the following output:

      pi = 3.1429
     
    pi = +3.143
     
    pi = 3.143e+003

    Discussion

    Manipulators that specifically manipulate floating-point output divide into two categories. There are those that set the format, which, for the purposes of this recipe, set the general appearance of floating-point and integer values, and there are those that fine-tune the display of each format. The formats are as follows:

    Normal (the default)

    In this format, the number of digits displayed is fixed (with a default of six) and the decimal is displayed such that only a set number of digits are displayed at one time. So, by default, pi would be displayed as 3.14286, and pi times 100 would display 314.286.

    Fixed

    In this format, the number of digits displayed to the right of the decimal point is fixed, while the number of those displayed to the left is not. In this case, again with a default precision of six, pi would be displayed as 3.142857, and pi times 100 would be 314.285714. In both cases, the number of digits displayed to the right of the decimal point is six while the total number of digits can grow indefinitely.

    Scientific

    The value is shown as a single digit, followed by a decimal point, followed by a number of digits determined by the precision setting, followed by the letter "e" and the power of ten to raise the preceding value to. In this case, pi times 1,000 would display as 3.142857e+003.

    Table 10-2 shows all manipulators that affect floating-point output (and sometimes numeric output in general). See Table 10-1 for general manipulators you can use together with the floating-point manipulators.

    Table 10-2. Floating-point and numeric manipulators

     
    Manipulator Description Sample output
    Fixed

    Show floating-point values with a fixed number of digitsto the right of the decimal point. 

    With a default precision of six digits:

      pi = 3.142857
    scientific Show floating-point values using scientific notation, which means a decimal number and an exponent multiplier.

    pi * 1000, with a default precision of six digits:

      pi = 3.142857e+003
    setprecision

    Control the number of digits displayed in the output. (See further explanation later.)

    Pi in the default format, with a precision of 3 :

      pi = 3.14
      In fixed format:
      pi = 3.143
      In scientific format:
      pi = 3.143e+000
    showpos

    Show a plus sign in front of positive numbers. 

    +3.14
    noshowpos This works for any kind of number, decimal or integer. 
    showpoint Show the decimal, even if there are only zeros after it.

    The following line, with a precision of 2 :

    noshowpoint This works only for floating-point values, and not for integers.  
      cout << showpoint << 2.0
      will display like this:
      2.00
    showbase Show the base for the number: decimal (none), octal (leading zero), or hexadecimal (leading 0x). See the next entry.Decimal: 32
    noshowbase  Octal: 040
      Hexadecimal: 0x20
    dec Set the base for the numbers to be displayed to decimal, octal, or hexadecimal. The base is not shown by default; use showbase to display the base. See previous entry.
    oct   
    hex   
    Uppercase Display values in uppercase. This sets the case for numeric out put, such as 0X for hexadecimal numbers and E for numbers in scientific notation.
    nouppercase   
       
       

    In all three formats, all manipulators have the same effects except setprecision. In the default mode, "precision" refers to the number of digits on both sides of the decimal point. For example, to display pi in the default format with a precision of 2, do this:

      cout << "pi = " << setprecision(2)
           << pi << '\n';

    Your output will look like this:

      pi = 3.1

    By comparison, consider if you want to display pi in fixed-point format instead:

      cout << "pi = " << fixed
           << setprecision(2)
           << pi << '\n';

    Now the output will look like this: 

      pi = 3.14

    This is because, in fixed-point format, the precision refers to the number of digits to the right of the decimal point. If we multiply pi by 1,000 in the same format, the number of digits to the right of the decimal remains unchanged:

      cout << "pi = " << fixed
           << setprecision(2)
           << pi * 1000 << '\n';

    produces:

      pi = 3142.86

    This is nice, because you can set your precision, set your field width with setw, right-justify your output with right (see Recipe 10.1 ), and your decimal points will all be lined up vertically.

    Since a manipulator is just a convenient way of setting a format flag on the stream, remember that the settings stick around until you undo them or until the stream is destroyed. Save the format flags (see Example 10-3) before you start making changes, and restore them when you are done

    See Also

    Recipe 10.3

    More C++ Articles
    More By O'Reilly Media


       · This article is an excerpt from the "C++ Cookbook," published by O'Reilly. We hope...
     

    Buy this book now. 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). Check it out today at your favorite bookstore. Buy this book now.

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek