C++ basic input and output


The C++ standard library provides a rich set of input/output functions, which we will introduce in subsequent chapters. This chapter discusses the most basic and common I/O operations in C++ programming.

C++ I/O occurs in streams, which are sequences of bytes. If a stream of bytes is flowing from a device (such as a keyboard, disk drive, network connection, etc.) to memory, this is called an input operation. If the byte stream is flowing from memory to a device (such as a display, printer, disk drive, network connection, etc.), this is called an output operation.

I/O library header files

The following header files are very important in C++ programming.

Header fileFunction and description
<iostream>This file The cin, cout, cerr and clog objects are defined, corresponding to the standard input stream, standard output stream, unbuffered standard error stream and buffered standard error stream respectively.
<iomanip>The file is passed through so-called parameterized flow manipulators such as setw and setprecision ), to declare services useful for performing standardized I/O.
<fstream>This file serves user-controlled file processing declarations. We'll discuss the details of this in the relevant chapters on files and streams.

Standard output stream (cout)

Predefined object cout is an instance of the ostream class. The cout object "connects" to the standard output device, usually the display screen. cout is used in conjunction with the stream insertion operator << as shown below:

#include <iostream>
 
using namespace std;
 
int main( )
{
   char str[] = "Hello C++";
 
   cout << "Value of str is : " << str << endl;
}

When the above code is compiled and executed, it produces the following results:

Value of str is : Hello C++

The C++ compiler selects the appropriate stream insertion operator to display the value based on the data type of the variable to be output. The << operator is overloaded to output data items of built-in types (integer, float, double, string, and pointer).

Stream insertion operator << can be used multiple times in a statement, as shown in the example above, endl is used to add a newline character at the end of the line.

Standard input stream (cin)

The predefined object cin is an instance of the istream class. The cin object is attached to the standard input device, usually the keyboard. cin is used in conjunction with the stream extraction operator >> as shown below:

#include <iostream>
 
using namespace std;
 
int main( )
{
   char name[50];
 
   cout << "请输入您的名称: ";
   cin >> name;
   cout << "您的名称是: " << name << endl;
 
}

When the above code is compiled and executed, it prompts the user for a name. When the user enters a value and presses the Enter key, he or she will see the following result:

请输入您的名称: cplusplus
您的名称是: cplusplus

The C++ compiler selects the appropriate stream extraction operator to extract the value based on the data type of the value to be entered, and puts It is stored in the given variable.

Stream extraction operator>> can be used multiple times in one statement. If multiple data inputs are required, the following statement can be used:

cin >> name >> age;

This is equivalent to the following two statements:

cin >> name;
cin >> age;

Standard error stream (cerr)

Predefined object cerr is an instance of the ostream class. The cerr object is attached to the standard error device, usually the display screen, but cerr objects are unbuffered, and each stream inserted into cerr is output immediately.

cerr is also used in conjunction with the stream insertion operator << as shown below:

#include <iostream>
 
using namespace std;
 
int main( )
{
   char str[] = "Unable to read....";
 
   cerr << "Error message : " << str << endl;
}

When the above code is compiled and executed, it will Produces the following results:

Error message : Unable to read....

Standard log stream (clog)

The predefined object clog is an instance of the ostream class. The clog object is attached to the standard error device, usually the display screen, but clog objects are buffered. This means that each stream inserted into the clog is first stored in the buffer and is not output until the buffer fills up or the buffer is flushed.

clog is also used in conjunction with the stream insertion operator << as shown below:

#include <iostream>
 
using namespace std;
 
int main( )
{
   char str[] = "Unable to read....";
 
   clog << "Error message : " << str << endl;
}

When the above code is compiled and executed, it will Produces the following results:

Error message : Unable to read....

With these small examples, we can't tell the difference between cout, cerr, and clog, but when writing and executing large programs, the difference between them becomes very obvious. So good programming practice tells us to use the cerr stream to display error messages, and other log messages to use the clog stream to output.