C++ Basic Input/Output

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

C++ I/O occurs in streams, which are sequences of bytes. If the byte stream flows from a device (such as a keyboard, disk drive, network connection, etc.) to memory, this is calledInput Operations. If the byte stream flows from memory to a device (such as a display screen, printer, disk drive, network connection, etc.), this is calledOutput Operations。

I/O library header file

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

Header fileFunctions and Descriptions
<iostream>This file definescin、cout、cerrandclogobjects, corresponding to the standard input stream, standard output stream, unbuffered standard error stream, and buffered standard error stream, respectively.
<iomanip>This file uses so-called parameterized stream manipulators (such assetwandsetprecision) to declare services useful for performing standardized I/O.
<fstream>This file declares services for user-controlled file processing. We will discuss its details in the chapter on files and streams.

Standard output stream (cout)

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

Example

#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 according to the data type of the variable to be output to display the value. The << operator is overloaded to output data items of built-in types (integer, floating-point, double, string, and pointer).

The stream insertion operator << can be used multiple times in one statement, as shown in the example above,endlUsed to add a newline at the end of a line.

Standard input stream (cin)

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

Example

#include <iostream> using namespace std; int main( ) { char name[50]; cout << "Please enter your name:"; cin >> name; cout << "Your name is:" << name << endl; }

When the above code is compiled and executed, it prompts the user to enter a name. When the user enters a value and presses Enter, the following results will be seen:

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

The C++ compiler selects the appropriate stream extraction operator according to the data type of the value to be input, extracts the value, and stores it in the given variable.

The stream extraction operator >> can be used multiple times in one statement. If multiple data items need to be input, 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 objectscerrYesiostreamAn instance of the class. The cerr object is attached to the standard output device, usually also a display screen, butcerrThe object is unbuffered, and each stream insertion to cerr is output immediately.

cerris also used in conjunction with the stream insertion operator <<, as shown below:

Example

#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 produces the following results:

Error message : Unable to read....

Standard log stream (clog)

Predefined objectsclogYesiostreamAn instance of the class. The clog object is attached to the standard output device, usually also a display screen, butclogThe object is buffered. This means each stream insertion to clog is first stored in the buffer and will not be output until the buffer is filled or the buffer is flushed.

clogis also used in conjunction with the stream insertion operator <<, as shown below:

Example

#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 produces the following results:

Error message : Unable to read....

Through these small examples, we cannot distinguish the differences between cout, cerr, and clog, but when writing and executing large programs, the differences between them become very obvious. Therefore, good programming practice tells us to use the cerr stream to display error messages, and to output other log messages using the clog stream.


other extensions