In every program, some data is taken as input and generates the processed data as output following the input > process > output cycle. Therefore it is essential to know how to provide data as input and present the outcome in the desired form. C++ supports a rich set of I/O functions and operations to do this. As these functions use advanced features, programmers need to know much about them before implementing them in any program. It is to be noted that C++ supports all of C's rich sets of I/O functions. Programmers can use any of them within a C++ program. In this tutorial, you will learn how to manage the input/output capabilities of a C++ program.



input/output in C++

C++ does not have built-in input/output facilities. Instead, it left the I/O to the compiler as external library functions (such as  printf and scanf) in the stdio (standard input-output) library. The ANSI C standard formalized these IO functions into a Standard IO package (stdio.h). Likewise, C++ continues this approach and formalizes IO in iostream and fstream libraries.

Features of I/O in C++

  • C++ IO is type safe.
  • C++ IO operations are based on streams of bytes and are device independent.

Streams in C++

C++ IO is based on streams, a sequence of bytes flowing in and out of the programs. I/O systems in C++ are designed to work with various devices, including terminals, disks, and tape drives. The input-output system supplies an interface to programmers that is independent of the actual device being accessed. This interface is known as a stream. A stream is a sequence of bytes that acts either as a source from which input data can be obtained or as a destination to which output data can be sent. The source stream which provides data to the program is called the input stream, and the destination stream which receives output from the program is called the output stream. Follow the steps below to perform input and output:

  • Construct a stream object.
  • Connect (Associate) the stream object to an actual IO device
  • Perform input/output operations on the stream via the functions defined in the stream's public interface in a device-independent manner.
  • Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).
  • Free the stream object.

Unformatted input/output Operations

You have already used the cin and cout (pre-defined in the iostream file) for the input and output of data of various types. It has been made possible by overloading the operators << and >> to recognize all the basic C++ types.

  • cin standard input stream
  • cout standard output stream

Example:

#include <iostream>
using namespace std;

void main()
 {
    int g;
    cin>>g;
    cout << "Output is: "<< g;
 }

put() and get() Functions

The classes istream and ostream define two member functions, get() and put(), respectively, to handle single-character input/output operations. get() function is of two types:

  1. get(char *)
  2. get(void)

Both functions can 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);
}

Similarly, the function put(), a member of a stream class, can be used to output a line of text character by character.

Example:

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

getline() and write() Functions

You can read and display lines of text more efficiently using the line-oriented input/output functions. They are:

  • getline()
  • write()

The getline() function reads the entire line of texts that ends with a newline character. The general form of getline() is:

cin.getline (line, size);

The write() function displays the entire line of text, and the general form of writing this function is:

cout.write (line, size);


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram