Streams in C++

Streams in C++: In this language input-output (io) is based on streams. C++ streams are nothing but the bytes which are flowing in and out of the programs. The input and output system supplies an interface to the programmer that is independent of the actual device being accessed.  This interface is known as a “stream”.

Streams in C++

The i/o streams in C++ are designed to work with a wide variety of devices including terminals, disks & tapes drivers. And the source stream which provides data to the program is called the “input stream”. While the destination stream which receives output from the program is called the “Output stream”.

put( ) and get( ) functions

The two classes istream and ostream define the two member functions i.e. get( ) and put( ) for handling single character input and output operations.



Get( ): get( ) function is of two types. They are 1) get(char*) 2)getvoid( )

  • Both of these functions can be used to fetch a character including blank space, tab (or) new-line character.

Example:

char ch;
cin.get(ch);
 while(ch != '\n')
  {
    cout<<ch;
    cin.get(ch);
  }

Similar to this we are having the put( ), as a member of a class that can be used to output the line text character by character.

Example:

cout.put ('g');
char ch;
cout.put(ch);

getline( )

getline( ) function helps us to read the entire line of the texts that ends with a  newline character.

Example:

cin.getline (line, size);

write( )

write( ) function helps us to display the entire line of text.

Example:

cout.write(line, size);