Skip to main content
Engineering LibreTexts

11.1: C++ lnput-Output Streams

  • Page ID
    29098
  • Basic Input / Output in C++

    C++ comes with libraries which provides us with many ways for performing input and output. In C++ input and output is performed in the form of a sequence of bytes or more commonly known as streams.

    • Input Stream: If the direction of flow of bytes is from the device(for example, Keyboard) to the main memory then this process is called input.
    • Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device( display screen ) then this process is called output.

    Depiction of the 4 default streams associated with C++ code
    Header files available in C++ for Input/Output operations are:

    1. iostream: iostream stands for standard input-output stream. This header file contains definitions to objects like cin, cout, cerr etc.
    2. iomanip: iomanip stands for input output manipulators. The methods declared in this files are used for manipulating streams. This file contains definitions of setw, setprecision etc.
    3. fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.

    The two keywords cout in C++ and cin in C++ are used very often for printing outputs and taking inputs respectively. These two are the most basic methods of taking input and printing output in C++. To use cin and cout in C++ one must include the header file iostream in the program.

    This lesson mainly discusses the objects defined in the header file <iostream> like cin and cout.

    cin and cout are NOT reserved words, they are actually variables, instances of classes, that have been declared in <iostream>. cout is a variable of type ostream. cin is a variable of type istream.

     

    1. Standard output stream (cout): Usually the standard output device is the display screen. The C++ cout statement is the instance of the ostream class. It is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).filter_none
      #include <iostream> 
      using namespace std; 
      
      int main() 
      { 
          char sample[] = "GeeksforGeeks"; 
      
          cout << sample << " - A computer science portal for geeks" << endl; 
      
          return 0; 
      } 
      

      In the above program the insertion operator(<<) inserts the value of the string variable sample followed by the string “A computer science portal for geeks” in the standard output stream cout which is then displayed on screen. Notice that you have to insert spaces in the second string. The output of the above program would be:

      GeeksforGeeks - A computer science portal for geeks

       

    2. standard input stream (cin): Usually the input device in a computer is the keyboard. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard.
      The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keboard.filter_none
      #include <iostream> 
      using namespace std; 
      
      int main() 
      { 
          int age; 
      
          cout << "Enter your age:"; 
          cin >> age; 
          cout << "Your age is: " << age << endl; 
      
          return 0; 
      } 
      

      The above program asks the user to input the age. The object cin is connected to the input device. The age entered by the user is extracted from cin using the extraction operator(>>) and the extracted data is then stored in the variable age present on the right side of the extraction operator.

      Input :

      18

      Output:

      Enter your age:
      Your age is: 18

      The other 2 streams are unbuffered. The difference between buffered and unbuffered streams is that a buffered stream does not immediately send the data to the destination, but instead, it buffers incoming data and then sends it in blocks. An unbuffered stream, on the other hand, immediately sends the data to the destination. Buffering is usually done to improve performance, as certain destinations, such as files, perform better when writing bigger blocks at once

    3. Un-buffered standard error stream (cerr): The C++ cerr is the standard error stream which is used to output the errors. This is also an instance of the iostream class. As cerr in C++ is un-buffered so it is used when one needs to display the error message immediately. It does not have any buffer to store the error message and display later.
      #include <iostream> 
      using namespace std; 
      
      int main() 
      { 
          cerr << "An error occured"; 
          return 0; 
      } 
      

      cerr by defaults will write to the console. 

      Output:

      An error occured
      
    4. buffered standard error stream (clog): This is also an instance of iostream class and used to display errors but unlike cerr the error is first inserted into a buffer and is stored in the buffer until it is not fully filled. The error message will be displayed on the screen too.filter_none
      #include <iostream> 
      using namespace std; 
      
      int main() 
      { 
          clog << "An error occured"; 
      
          return 0; 
      } 
      

      Again, this stream/s default device is the console. 

      Output:

      An error occured

    Adapted from:
    "Basic Input / Output in C++" by Harsh AgarwalGeeks for Geeks is licensed under CC BY-SA 4.0 

    • Was this article helpful?