Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Basic Input/Output

C++ Basic Input/Output

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

C++ I/O occurs in streams, where a stream is a sequence of bytes. If the byte stream flows from a device (such as a keyboard, disk drive, network connection, etc.) into memory, this is calledinput operation. If the byte stream flows from memory to a device (such as a display screen, printer, disk drive, network connection, etc.), this is calledoutput operation.

I/O Library Header Files

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

Header File Functions and Description
This file defines the cin, cout, cerr and clog objects, corresponding respectively to the standard input stream, standard output stream, unbuffered standard error stream, and buffered standard error stream.
This file, through so-called parameterized stream manipulators (such as setw and setprecision), declares services useful for performing standardized I/O.
This file declares services for user-controlled file handling. We will discuss its details in the chapter on files and streams.

Standard Output Stream (cout)

The predefined object cout Is iostream is an instance of the class. The cout object is "connected" to the standard output device, usually the display screen.cout is used in combination 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 code above is compiled and executed, it produces the following result:


Value of str is : Hello C++

The C++ compiler selects the appropriate stream insertion operator to display a 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, floating-point, double, string, and pointer).

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

Standard Input Stream (cin)

The predefined object cin Is iostream is an instance of the class. The cin object is attached to the standard input device, usually the keyboard.cin is used in combination 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 code above is compiled and executed, it prompts the user to enter a name. When the user enters a value and presses Enter, the following result is seen:


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

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

The stream extraction operator >> can be used multiple times in a single statement; if multiple data items are required as input, you can use the following statement:


cin >> name >> age;

This is equivalent to the following two statements:


cin >> name;
cin >> age;

Standard Error Stream (cerr)

The predefined object cerr Is iostream is an instance of the class. The cerr object is attached to the standard output device, usually also the display screen, but cerr is unbuffered, and each stream inserted into cerr is output immediately.

cerr is also used in combination 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 code above is compiled and executed, it produces the following result:


Error message : Unable to read....

Standard Log Stream (clog)

The predefined object clog Is iostream is an instance of the class. The clog object is attached to the standard output device, usually also the display screen, but clog is buffered. This means each stream inserted into clog is first stored in a buffer and is only output when the buffer is filled or flushed.

clog is also used in combination 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 code above is compiled and executed, it produces the following result:


Error message : Unable to read....

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