C++: I/O Operations -1

C++ Streams

C++ is designed to work with a wide variety of devices. The I/O system supplies an interface to the programmer that is independent of the actual device being used.

A stream is a sequence of bytes.

The source stream that provides data to the program is called input stream.

The destination stream that receives output from program is called output stream.

A program extracts bytes from an input stream and inserts bytes into an output stream.

C++ Stream Classes

The classes are ios, istream, ostream, iostream, streambuf .

The class ios is declared as the virtual base class so that only one copy of its members are inherited by the iostream.

istream provides the facilities for formatted and unformatted input.

ostream provides the facilities for formatted output.

Unformatted I/O Operations

Overloaded Operators >> and <<:

The  >> operator is overloaded in the istream class and << is overloaded in the ostream class.


This statement will cause the computer to stop the execution and look for input from the keyboard .

The operator >> reads the data charcater by character and assigns it to the indicated location.

The reading for a variable will be terminated at the encounter of a white space or a character that does not match the destination type.

put() and get() functions:

The class istream defines get() to handle single character input operation and ostream defines put() to handle single character output operation.

We can also use get(char*) and get(void).

get(char*) version assigns the input character to its argument and the get(void) version returns the input character.

  char c;
  cin.get(c); //get a character from keyboard and assign it to c

Operator >> can also be used to read a character but it will skip the white spaces and newline character.

We can also use

  cout.put('x'); displays the character x

  cout.put(ch); displays the value of variable ch.

  cout.put(68); displays the character D.

getline() and write() functions

The getline() function reads a whole line of text that ends with a newline character(transmitted by the RETURN KEY).


The reading is terminated as soon as either the newline character '\n' is encountered or size-1 characters are read(whichever occurs first).

The newline character is read but not saved.

cout.write(line, size)

It does not stop displaying the characters automatically when the null character is encountered. If the size is greater than the length of line, then it displays beyond the bounds of line.

            < Happy Coding >

In Association with Amazon.in


Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

How do I improve my logical thinking and problem solving skills?

C++: STL Iterators