13.1 - Input and Output (I/O) Streams: by A Lex, On Februa Ry 28Th, 2008
13.1 - Input and Output (I/O) Streams: by A Lex, On Februa Ry 28Th, 2008
Input and output functionality is not defined as part of the core C++ language, but rather is provided through the C++ standard library (and thus resides in the std namespace). In previous lessons, you included the iostream library header and made use of the cin and cout objects to do simple I/O. In this lesson, well take a look at the iostream library in more detail. The iostream library When you include the iostream header, you gain access to a whole heirarchy of classes responsible for providing I/O functionality (including one class that is actually named iostream). The class heirarchy for the non-file-I/O classes looks like this:
The first thing you may notice about this hierarchy is that it uses multiple inheritance (that thing we told you to avoid if at all possible). However, the iostream library has been designed and extensively tested in order to avoid any of the typical multiple inheritance problems, so you can use it freely without worrying. Streams
The second thing you may notice is that the word stream is used an awful lot. At its most basic, I/O in C++ is implemented with streams. Abstractly, a stream can be thought of as a sequence of bytes of infinite length that is used as a buffer to hold data that is waiting to be processed. Typically we deal with two different types of streams. Input streams are used to hold input from a data producer, such as a keyboard, a file, or a network. For example, the user may press a key on the keyboard while the program is currently not expecting any input. Rather than ignore the users keypress, the data is put into an input stream, where it will wait until the program is ready for it. Conversely, output streams are used to hold output for a particular data consumer, such as a monitor, a file, or a printer. When writing data to an output device, the device may not be ready to accept that data yet for example, the printer may still be warming up when the program writes data to its output stream. The data will sit in the output stream until the printer begins consuming it. Some devices, such as files and networks, are capable of being both input and output sources. The nice thing about streams is the programmer only has to learn how to interact with the streams in order to read and write data to many different kinds of devices. The details about how the stream interfaces with the actual devices they are hooked up to is left up to the environment or operating system. Input/output in C++ Although the ios class is generally derived from ios_base, ios is typically the most base class you will be working directly with. The ios class defines a bunch of stuff that is common to both input and output streams. Well deal with this stuff in a future lesson. The istream class is the primary class used when dealing with input streams. With input streams, the extraction operator (>>) is used to remove values from the stream. This makes sense: when the user presses a key on the keyboard, the key code is placed in an input stream. Your program then extracts the value from the stream so it can be used. The ostream class is the primary class used when dealing with output streams. With output streams, the insertion operator (<<) is used to put values in the stream. This also makes sense: you insert your values into the stream, and the data consumer (eg. monitor) uses them. The iostream class can handle both input and output, allowing bidirectional I/O. Finally, there are a bunch of classes that end in _withassign. These stream classes are derived from istream, ostream, and iostream (respectively) with an assignment operator defined, allowing you to assign one stream to another. In most cases, you wont be dealing with these classes directly. Standard streams in C++ A standard stream is a pre-connected stream provided to a computer program by its environment. C++ comes with four predefined standard stream objects that have already been set up for your use. The first two, you have seen before: 1. cin an istream_withassign class tied to the standard input (typically the keyboard) 2. cout an ostream_withassign class tied to the standard output (typically the monitor) 3. cerr an ostream_withassign class tied to the standard error (typically the monitor), providing unbuffered output 4. clog an ostream_withassign class tied to the standard error (typically the monitor), providing buffered output
Unbuffered output is typically handled immediately, whereas buffered output is typically stored and written out as a block. Because clog isnt used very often, it is often omitted from the list of standard streams. A basic example Heres an example of input and output using the standard streams: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 # i n c l u d e< i o s t r e a m > i n tm a i n ( ) { u s i n gn a m e s p a c es t d ; / /F i r s tw e ' l lu s et h ei n s e r t i o no p e r a t o ro nc o u tt op r i n tt e x tt ot h em o n i t o r c o u t< <" E n t e ry o u ra g e :"< <e n d l ; / /T h e nw e ' l lu s et h ee x t r a c t i o no p e r a t o ro nc i nt og e ti n p u tf r o mt h eu s e r i n tn A g e ; c i n> >n A g e ; i f( n A g e< =0 ) { / /I nt h i sc a s ew e ' l lu s et h ei n s e r t i o no p e r a t i o ro nc e r rt op r i n ta ne r r o rm e s s a g e c e r r< <" O o p s ,y o ue n t e r e da ni n v a l i da g e ! "< <e n d l ; e x i t ( 1 ) ; } / /O t h e r w i s ew e ' l lu s ei n s e r t i o na g a i no nc o u tt op r i n tar e s u l t c o u t< <" Y o ue n t e r e d"< <n A g e< <"y e a r so l d "< <e n d l ; } r e t u r n0 ;
In the next lesson, well take a look at some more I/O related functionality in more detail.
Index
12.6 Pure virtual functions, abstract base classes, and interface classes
C ++ TU TOR IAL |
PR IN T TH IS POST
ivailosp
March 3, 2008 at 8:20 pm Log in to Reply simple program using stringstream # i n c l u d e # i n c l u d e u s i n gn a m e s p a c es t d ; i n ts e x y ( c o n s tc h a r *a ,c o n s tc h a r *b ){ s t r i n g s t r e a mt m p ; t m p<<a ; i n tt m p 1 ; t m p> >t m p 1 ; t m p . c l e a r ( ) ; t m p<<b ; i n tt m p 2 ; t m p> >t m p 2 ; r e t u r nt m p 1 + t m p 2 ; } i n tm a i n ( ){ c h a r *s t r i n g _ o n e=" 2 1 2 3 " ; c h a r *s t r i n g _ t w o=" 2 3 4 " ; c o u t<<s e x y ( s t r i n g _ o n e ,s t r i n g _ t w o ) ; r e t u r n0 ; }
Learn C++ - 12.6 Pure virtual functions, abstract base classes, and interface classes
May 4, 2008 at 6:27 pm Log in to Reply [...] 13.1 Input and output (I/O) streams [...]
Giorgos
June 30, 2008 at 6:12 pm Log in to Reply
you got a mistake here! x is not mentioned #include <iostream> int main() { using namespace std; cout << "Enter your age: " << endl; int nAge; cin >> nAge; if (nAge <= 0) { cerr << "Oops, you entered an invalid age!" << endl; exit(1); } cout << "You entered " << nAge << " years old" << endl; return 0; } [ Fixed. -Alex ]
rohan
September 16, 2008 at 5:15 am Log in to Reply Abstractly, a stream can . infinite length that us used as a buffer ===> Abstractly, a stream can . infinite length that is used as a buffer [ Fixed. Thanks! -Alex]
Kavitha
January 22, 2009 at 7:39 am Log in to Reply cout an ostream_withassign class tied to the standard input should read as cout an ostream_withassign object tied to the standard output [ You are correct. Thanks for pointing out the mistake. -Alex ]
Abhisek
March 21, 2009 at 11:31 am Log in to Reply Q: What is the difference between the outputs of cout, cerr & clog ? NOTE: Both of them gives exactly the same output to the screen. Q: Does cerr has any more inner workings than printing Oops, you entered an invalid age! to the screen? c o u t< <" O o p s ,y o ue n t e r e da ni n v a l i da g e ! "< <e n d l ;
Alex
May 1, 2009 at 8:01 pm Log in to Reply Theres actually very little difference between them. However, it is possible to redirect one or more of the channels to file (or printer, or network, or elsewhere). That way, you can have normal output go to one place (eg. a file) and have any errors go to another place (eg. the screen).
Alex
May 3, 2009 at 7:13 am Log in to Reply In your second line in after Streams, you have its everyone knows possessive pronouns cannot have an apostrophe!
saini
August 30, 2010 at 9:02 pm Log in to Reply
w h a ta r eb u f f e r e da n du n b u f f e r e de r r o r s?
sloooh
May 11, 2011 at 8:01 am Log in to Reply i have qustion , any one can help me???
sloooh
May 11, 2011 at 8:06 am Log in to Reply Input three positive integers representing the sides of a traingle and determine whether they form a valid traingle. Hint:n a traingle the sum of any two sides must always be greater than the side. how i can make programme for this by c++ way???
Nick Xu
May 20, 2011 at 8:12 pm Log in to Reply OK,do it like this: //C style without C++ class,under VC++6.0 #include
using namespace std; int main() { int a,b,c; do{ printf(a,b,c:\n); scanf(%d,%d,%d,&a,&b,&c); }while(!(a+b>c && a+c>b && c+b>a)); printf(Correct!A valid traingle.\n); return 0; }
Nick Xu
May 20, 2011 at 8:15 pm Log in to Reply //run correctly #include using namespace std; int main() { int a,b,c; do{ printf(a,b,c:\n); scanf(%d,%d,%d,&a,&b,&c); }while(!(a+b>c && a+c>b && c+b>a)); printf(Correct!A valid traingle.\n); return 0; }