May 11, 2021 C++
The standard library provides a rich set of input/output features, which we'll cover in a later section. This chapter discusses the most basic and common I/O operations in C?programming.
The I/O of C++ occurs in a stream that is a sequence of bytes. T his is called an input operation if the byte stream flows from the device (such as keyboard, disk drive, network connection, and so on). This is called output operations if the byte stream flows from memory to the device, such as displays, printers, disk drives, network connections, and so on.
The following header files are important in C++ programming.
head File | Function and description |
---|---|
<iostream> | This file is defined cin、cout、cerr with clog Objects respectively correspond to standard input streams, standard output flow, non-buffered standard error flow, and buffer standard error flow. |
<iomanip> | This document passes the so-called parameterized rigider (such as setw with setprecision ) To declare the services that perform standardized I / O useful services. |
<fstream> | This document processing declaration service for user-controlled file processing.We will discuss its details in the relevant chapters of the file and stream. |
The predefined object cout is an instance of the ostream class. T he cout object is "connected" to a standard output device, usually a display. cout is used in conjunction with the stream insertion operator, which looks like this:
#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 output. The operator is overloaded to output data items of built-in types (integers, floats, doubles, strings, and pointers).
The stream insert operator, the flow insertion operator, can be used multiple times in a statement, as shown in the example above, and endl is used to add a line break at the end of the line.
The predefined object cin is an instance of the istream class. T he cin object is attached to a standard input device, usually a keyboard. cin is used in conjunction with the stream extraction operator, as follows:
#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, the following results are seen:
请输入您的名称: cplusplus 您的名称是: cplusplus
Depending on the type of data you want to enter the value, the C++ compiler selects the appropriate stream extraction operator to extract the value and store it in a given variable.
The stream extraction operator can be used more than once in a statement, and if you require more than one data, you can use the following statement:
cin >> name >> age;
This is equivalent to the following two statements:
cin >> name;
cin >> age;
The predefined object cerr is an instance of the ostream class. The cerr object is attached to a standard error device and is usually a display, but the cerr object is non-buffered and each stream inserted into the cerr outputs immediately.
cerr is also used in conjunction with the stream insertion operator, slt;lt; as follows:
#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....
A predefined object clog is an instance of an ostream class. T he clog object is attached to a standard error device and is usually a display, but the clog object is buffered. This means that each stream inserted into the clog is stored in the buffer until the buffer is full or the buffer is flushed.
The clog is also used in conjunction with the stream insertion operator, slt;lt; as follows:
#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....
With these small instances, 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 that cerr streams are used to display error messages, while other log messages are output using clog streams.