Streams and Files - Intro
The following topics are covered in this section:
Introduction to Streams
You might have heard of I/O. It stands for Input-Output. Before dealing directly with files we shall first take a look at streams. Using C++ you can access a device through an intermediate medium. The intermediate medium is called a stream. The device that you access is known as file.
Don't mistake the term file as the normal computer file. A normal file (a text file or a word document) is referred to as disk file. When dealing with streams, the term file takes a broader meaning. It includes disk files, tape drives, the terminal, printers and other devices. C++ is designed to interact with these devices and this can be accomplished using streams.
Each device is unique in its own way. The C++ file system transforms each device into a stream. All streams will behave similarly though they provide access to different devices (or files). Hence streams are mostly device-independent. The function used to write to a disk file can be used to write to the monitor screen as well. Hence all streams are similar but all files are not.
Each stream is associated with a specific file by using the open operation. Once you open a file (not just disk file – it could even be a printer), information can be exchanged between the file and your C++ program. This exchange is done through the medium of streams. A stream is an object and there are different kinds of general I/O streams:
These are general I/O streams and they are not specifically for disk file operations. We shall deal with file I/O later.
The iostream class has two overloaded operators:
These operators (which are actually bitwise shifting operators) are already overloaded to work with fundamental data types and a programmer can also overload them to work with objects (user-defined data).
Overloading insertion and extraction operators
The insertion (or output operator <<) and extraction operators (or input operator >>) are binary operators that are already overloaded to operate on built-in data types. The insertion operator will insert data into a stream while the extraction operator is used to extract data from a particular stream. Suppose we had a class ‘car’ and we created an object called ‘ford’, wouldn’t it be wonderful if we could just type:
and the program should display the values of the object ford. Of course we can make use of a member function to display this (in fact that is what we’ve been doing so far) but you can overload the << operator to perform such operations.
If you notice the statement, one operand is an object and one operand is a stream. ‘cout’ is also an object but it won’t be belonging to the class that we create. The << will be overloaded in our class but since it is in the right hand side, it won’t be able to invoke the operator overloading function. This problem can be solved by using friend functions for overloading the << and >> operators. We have seen this in the chapter on operator overloading but since we are dealing with streams, one of the arguments to the friend function should be a stream.
The output from the above program will be:
Enter the speed: 280
Enter the colour: red
The speed is: 280
The color is: red
You just have to remember that you will need to use a stream as one of the parameters in the operator overloading function. By the way, cout is an object of type ‘ostream’. ‘cin’ is an object of type ‘istream’. In the above program you could write:
stream<<"\nThe speed is: "<<ob.speed;
cout<<"\nThe speed is: ";
because here ‘stream’ is going to correspond to ‘cout’.
Wouldn’t it be even more wonderful if we could say:
instead of creating a member function called ‘set’ for this purpose? This can be achieved by overloading the input operator >> as shown below:
The output from the above program will be:
Enter the speed: 300
Enter the colour: blue
The speed is: 300
The colour is: blue
As you would have noticed, in the above program we have overloaded the insertion as well as extraction operator. In this way you can make use of >> and << to work on your objects.
Reference: There exists a bug in VC++ compiler when using friend functions to overload the << and >> operators. Refer to question 9 in Chapter 14 for details.
Pre-defined Stream objects
In C++ there are 4 streams which are already defined. These streams are opened when you run a C++ program. They are:
|clog||Buffered error display||Monitor|
Buffering and flushing will be dealt with in the next chapter.
The standard streams described above can be redirected to other devices or files. The ‘cerr’ object is similar to the ‘cout’ object but the difference is that even if ‘cerr’ is redirected by the user to some other device the error message will be displayed on the console (i.e. on the monitor screen).
Go back to the Contents Page 2
Copyright © 2004 Sethu Subramanian All rights reserved.