Unformatted IO Operations

Overloaded Operators:

The >> operator is overloaded in the istream class and << is overloaded in the ostream class. The following is the general format for reading data from the keyboard

Example:

cin >> variable 1 >> variable 2 >>… …>> variable N;

Where variable1, variable2,…. are valid C++ variable names that have been declared already. This statement will cause the computer to halt the execution and look for input data from the keyboard.

C++ is designed to work with a wide variety of devices including terminals, disks, and tape drives. Although each device is very different, the system supplies an interface to the programmer that is independent of the actual device being accessed, This interface is known as stream.

Functions for Unformatted IO Operations

The classes istream and ostream define different types of functions respectively to handle the input/output operations. Below is the list of functions with syntax and use.

FunctionSyntaxUse
coutcout<<” ”<<” ”;To display character, string and number on output device.
cincin>> var1>>var2;To read character, string and number from input device.
get(char*)char ch;To read character including blank space, tab and newline
 cin.get(ch);character from input device. It will assign input character to its argument.
get(void)char ch; ch=cin.get();To read character including blank space, tab and newline character from input device. It will returns input character.
put()char ch; cout.put(ch);To display single character on output device. If we use a number as an argument to the function put(), then it will convert it into character.
getline()char name[20]; int size=10; cin.getline(name,size);It is used to reads a whole line of text that ends with a newline character or size -1 character. First argument represents the name of string and second argument indicates the number of character to be read.
write()char name[20]; int size=10; cout.write(name,size);It is used to display whole line of text on output device. First argument represents the name of string and second argument indicates the number of character to be display.

Example:

char c;
cin.get (c); //get a character from keyboard and assign it to c
while (c!= '\n')
{
	cout << C;   //display the character on screen 
	cin.get (c); //get another character
}

This code reads and displays a line of text (terminated by a newline character). Remember, the operator >> can also be used to read a character but it will skip the white spaces and newline character. The above while loop will not work properly if the statement.

cin >> c;
   // is used in place of
cin.get (c);

Example of cin and cout:

#include<iostream> 
using namespace std; 
int main()
{
int a;
cout<<"Enter the number"; 
cin>>a;
cout<<"The value of a="<<a; 
return 0;
}

Example of get(char*), char(void) and put():

#include <iostream> 
using namespace std; 
int main()
{
	int a=65; 
	char ch;
	cin.get(ch);	//get(char*) 
	cout.put(ch);		//put() 
	ch=cin.get(); //get(void) 
	cout.put(ch);
	cout.put(a); 
	return 0;
}

Example of getline() and write():

#include<iostream> 
using namespace std; 
int main()
{
   int size=5; 
   char name[50];
   cin.getline(name,size);	//getline() 
   cout.write(name,size);	//write 
   return 0;
}

Related posts