C++
  Home arrow C++ arrow Page 4 - General Stream Manipulation in 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++

General Stream Manipulation in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-04-28

    Table of Contents:
  • General Stream Manipulation in C++
  • Numeric Systems and Bool Values
  • Float Formatting
  • Closing Thoughts

  • 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


    General Stream Manipulation in C++ - Closing Thoughts


    (Page 4 of 4 )

    Up to this point I have given you examples of the output part, so I have decided to leave this last section to the input part. One of the most interesting manipulators in the previous section is the black sheep setw.

    This also works for the cin, as it is a member of the ios_base. However, there is a similar function that will do the same thing—let’s me introduce it now.

    The insertion operator is no longer valid for a pure ifstream function, so we must call it explicitly or via the insertion operator, as I will present it in the instance below:


    #include <iostream>

    #include <iomanip>

    #include <string>

    using namespace std;


    int main()

    {

    istream::fmtflags oldflag = cin.flags();

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

    string text;

    //char text[10];


    cout << setfill(')');

    cout << " enter something: ";

     

    cin.width(4);

     

    while ( cin >> text)

    {

    cin.width(3);

     

    cout.width(4);

    cout << text << endl;

    }

     

    cin.flags(oldflag);

    cout.flags(ooldflag);

    return 0;

    }


    enter something: I am writing some code

    )))I

    ))am

    )wri

    ))ti

    ))ng

    )som

    )))e

    )cod

    )))e


    Now there is an interesting way to format your code, or simply split it up into smaller inputs. The width function acts just like the setw, and it only works for the next input/output.

    Additionally, if you are bored with being interrupted by the white spaces to read in a long line, you can decide to overwrite the white space skip. This way, you can read the text char-by-char, or read it all at once. The manipulator you need to use is skipws:


    cin >> skipws; // skip the white spaces and look at them //as delimitations

    cin >> noskipws; //return to default


    With this we have covered all that is to be found within the manipulators already included in the STL. In future articles we will look at the low-level sequence reading of files, but before that we'll finish our work here with Custom Stream Manipulating in C++, so look for it.

    Thank you for reading my article. I invite you to ask any question you might have here on the blog below or to join our friendly, ever-growing forum over at DevHardware and present your question there. Until the next time we meet, remember to “Live with Passion!”


    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.

     

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek