Get ( )and Put ( )


Get and Put functions read and write unformatted data. Both these functions operate on single characters only.
Get ( ) is used to read a character while put ( ) is used to write a character.
It's easier to explain with the help of an example.


// Program to create a file, write to it and then display the contents of the file

#include <iostream.h>
#include <fstream.h>
int main ( )
{
ofstream out( "test.txt", ios::out | ios::binary );       
// Stream opened for writing - that is in output mode.
char letter;
cout<<"Enter what you want to store: ";
cin>>letter ;
while ( letter! = ' * ' )                                   
// The body keeps on repeating as long as you don't type *
    {
    out.put( letter );                         
// put ( ) function puts the letter into the stream  (i.e actually into the file)
    cin>>letter;                            
// This is to get the next entry into the file. 
    }
out.close( );
char ch;
cout<<endl<<"Contents of file are : ";
ifstream in ("test.txt", ios::in | ios::binary);       
// Stream opened for reading the contents of the file
while(in)                                                   
// The loop keeps repeating till end of file is reached.
    {
    in.get(ch);                                   
// We get a character from the stream.
        if (in)                                       
// Body of if executed if it is not end of file.
        {
        cout<<ch;                           
// The character obtained will be displayed on the screen
        }
    }
in.close( );
return 0;
}

The above program is simple to understand. Just try it out in your compiler. Put ( ), puts a character into the stream (actually into the file). Get ( ), gets a character from the stream. Put ( ) and get ( ) cannot be used individually. They are member functions of the stream classes and hence they have to be called using objects belonging to those classes. Till the compiler comes across an * in the input, it will keep storing all the characters.
Let us examine
while ( in )
The condition within the brackets becomes false only when the end of file is reached. So till the end of file is reached we keep getting the characters one by one and then displaying them one by one on the screen.

Assume that we have stored the word : qwerty in the file test.txt. When the compiler opens the file for reading, it has a small bookmarker that points at the beginning of the file. The bookmarker points at the letter q. It gets the letter q (using the get(ch) statement) and then displays it on the screen. The compiler knows it still hasn't reached the end of file. So, the next time it goes through the while loop, the bookmarker has moved to the next character (i.e to w). The process repeats till the end of file is reached.

Remember : You cannot save spaces (blank spaces) using put and get functions. Try it in the above program and see what I mean!!!


Go to the next section on : Read and Write Functions

or go back to Contents Page 2.