C++
  Home arrow C++ arrow Page 3 - Iostream Library and Basic I/O 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++

Iostream Library and Basic I/O in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-03-24

    Table of Contents:
  • Iostream Library and Basic I/O in C++
  • The Iostream Library
  • Basic I/O in C
  • Conclusion

  • 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


    Iostream Library and Basic I/O in C++ - Basic I/O in C


    (Page 3 of 4 )

    What each of the objects I mentioned at the end of the last section mean is quite simple. The cin is assigned to the input device of the system (stdin), while the cout does the same in the output direction (stdout). The two that remain are reserved for error reporting (stderr). While cerr offers an unbuffered version (though after every char a new flush), clog support is buffered, allowing the setting of a line of data through.

    The first one is resolved instantly, as there is no space for storing the problem, while the second can be resolved afterward. The clog is very rarely used; this is why it tends to be ignored.

    Now here is a little demonstration of how this will work in real life coding. Before you use them, you need to include theiostreamlibrary, and unless you want to point out at every step that this is a member of the STL by calling functions like std::cin/std::cout, you should also signal that we'll be using the STL namespace.


    #include<iostream>

    using namespace std;


    With the cin stream, the extraction operator will be used; that will be followed by what to extract as input.


    int justANumber;

    cin >> justANumber;


    With cout, the method is similar; we will insert something into the cout stream with the insertion (<<) operator. In addition, here we should mention that the end line is composed of more than one character. You probably know that in C you used the 'n' char (backslashfollowed by the lettern), which stands for new line.

    Nevertheless, whenever you hit an "enter" key, let us say in notepad, there will be an additional carriage return as well; that is represented by the 'r' character (backslashfollowed by the letterr). Therefore, in theiostreamlibrary, a constant contains all this and will flush the stream(std::endl). By this, we mean that it will make sure that the data are sent to the screen and are no longer waiting to be displayed.

    Sometimes this behavior is not desired. Since flushing the stream takes up a few processes, you may not want to print the result of every step on the screen; you may prefer to do it once at the end to speed up the execution time of the application. In these cases, avoid using std::endl --instead, write on the screen the character sequence "nr" (new line + carriage return).


    cout << justANumber << std::endl;


    As you see, you can add multiple items to the stream in a single line. This is possible because the extraction/insertion operator is also returning a reference to the stream, so in fact the upper lines look like this:


    (cout.operator<<(justAnumber))/*=cout*/.operator<<(std::endl);


    Here we arrive at the question of what will happen once you enter invalid data, such as text when you are expecting a number. Let's experiment.


    #include <iostream>


    int main()

    {

    int one, two;

    std::cin >> one >> two;

    std::cout.operator <<(one).operator <<(two).operator <<(std::endl); // == cout << one << two << endl;

    return 0;

    }


    With results:


    123AB

    123-858993460

    Press any key to continue . . .


    AB123

    -858993460-858993460

    Press any key to continue . . .


    From this, you may already know the answer. It will extract any valid data it can. Once an invalid input is received, the fail flag of the stream is set, and any future work with the stream will fail to complete.

    It is your duty to anticipate this kind of behavior and control it, or use it to your advantage. You may create a while loop to enter an indefinite number of rows of int until invalid input is entered:


    while(cin>> justANumber)

    {

    // do whatever you want with the input.

    }


    As I said before, the insertion operator will return the stream itself. In conclusion, after the insertion is complete, the loop will evaluate whether the statement between the parentheses is or is not true. In the case of a stream, this is reduced to the evaluation of the fail flag of the stream.

    However, if you want to enter a char sequence after this, you need to reset the stream by calling the clear function.


    if(std::cin.fail())

    std::cin.clear();

    //now we can resume the input operation


    You may also choose to ignore some chars if you know what is causing the problem, using the ignore function:


    cin.ignore(2); // ignore the first two character of the input


    More C++ Articles
    More By Gabor Bernat


     

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