Unformatted input/output operations In C++ - GeeksforGeeks

11 Nov, 2021
In this article, we will discuss the unformatted Input/Output operations In C++. Using objects cin and cout for the input and the output of data of various types is possible because of overloading of operator >> and << to recognize all the basic C++ types. The operator >> is overloaded in the istream class and operator << is overloaded in the ostream class.
The general format for reading data from the keyboard:
cin >> var1 >> var2 >> .... >> var_n;
Here, var1, var2, ......, varn are the variable names that are declared already.
The input data must be separated by white space characters and the data type of user input must be similar to the data types of the variables which are declared in the program.
The operator >> reads the data character by character and assigns it to the indicated location.
Reading of variables terminates when white space occurs or character type occurs that does not match the destination type.
Program 1:
C++
// C++ program to illustrate the// input and output of the data// entered by user#include <iostream>using namespace std; // Driver Codeint main(){    int data;    char val;     // Input the data    cin >> data;    cin >> val;     // Print the data    cout << data << "   " << val;     return 0;}
Output: 
Explanation: In the above program, 123 is stored in the variable val of integer, and B is passed to the next cin object and stored in the data variable of character. 
The class istream and ostream have predefined functions get() and put(), to handle single character input and output operations. The function get() can be used in two ways, such as get(char*) and get(void) to fetch characters including blank spaces, newline characters, and tab. The function get(char*) assigns the value to a variable and get(void) to return the value of the character.
Syntax: 
char data;
// get() return the character value and assign to data variabledata = cin.get();
// Display the value stored in data variablecout.put(data); 
Example: 
char c;
// directly assign value to c cin.get(c); 
// Display the value stored in c variablecout.put()
Program 2: 
C++
// C++ program to illustrate the// input and output of data using// get() and puts()#include <iostream>using namespace std; // Driver Codeint main(){    char data;    int count = 0;     cout << "Enter Data: ";     // Get the data    cin.get(data);     while (data != '\n') {        // Print the data        cout.put(data);        count++;         // Get the data again        cin.get(data);    }     return 0;}
Output:
In C++, the function getline() and write() provide a more efficient way to handle line-oriented inputs and outputs. getline() function reads the complete line of text that ends with the new line character. This function can be invoked using the cin object.
Syntax:
cin.getline(variable_to_store_line, size);
The reading is terminated by the ‘\n’ (newline) character. The new character is read by the function, but it does not display it, instead, it is replaced with a NULL character. After reading a particular string the cin automatically adds the newline character at end of the string.
The write() function displays the entire line in one go and its syntax is similar to the getline() function only that here cout object is used to invoke it.
Syntax:
cout.write(variable_to_store_line, size);
The key point to remember is that the write() function does not stop displaying the string automatically when a NULL character occurs. If the size is greater than the length of the line then, the write() function displays beyond the bound of the line.
Program 3:
C++
// C++ program to illustrate the// input and output of file using// getline() and write() function#include <iostream>#include <string>using namespace std; // Driver Codeint main(){    char line[100];     // Get the input    cin.getline(line, 10);     // Print the data    cout.write(line, 5);    cout << endl;     // Print the data    cout.write(line, 20);     cout << endl;     return 0;}
Output: 
 
kashishsoda
ak14aman
C++-Operator Overloading
cpp-input-output
cpp-operator
cpp-operator-overloading
Operators
C++
C++ Programs
cpp-operator
Operators
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
C++ Program for QuickSort
How to return multiple values from a function in C or C++?
Sorting a Map by value in C++ STL