Cplusplus 简明教程
C++ Basic Input/Output
C 标准库提供了一套广泛的输入/输出功能,我们将在随后的章节中看到。本章将讨论 C 语言编程所需的非常基础且最常用的 I/O 操作。
The C standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C programming.
C++ I/O 发生在流中,流是字节序列。如果字节从键盘、磁盘驱动器或网络连接等设备流向主存储器,则称为 input operation ;如果字节从主存储器流向显示屏、打印机、磁盘驱动器或网络连接等设备,则称为 output operation 。
C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called output operation.
I/O Library Header Files
以下标头文件对 C++ 程序很重要:
There are following header files important to C++ programs −
Sr.No |
Header File & Function and Description |
1 |
<iostream> This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively. |
2 |
<iomanip> This file declares services useful for performing formatted I/O with so-called parameterized stream manipulators, such as setw and setprecision. |
3 |
<fstream> This file declares services for user-controlled file processing. We will discuss about it in detail in File and Stream related chapter. |
The Standard Output Stream (cout)
预定义对象 cout 是 ostream 类的实例。cout 对象被认为是“连接到”标准输出设备(通常是显示屏)。 cout 与流插入运算符结合使用,流插入运算符写为 <<,如以下示例所示,它是两个小于号。
The predefined object cout is an instance of ostream class. The cout object is said to be "connected to" the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << which are two less than signs as shown in the following 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 result −
Value of str is : Hello C++
C++ 编译器还确定输出变量的数据类型,并选择适当的流插入运算符来显示值。<< 运算符重载为显示内建类型整数、浮点数、双精度数、字符串和指针值的数据项。
The C++ compiler also determines the data type of variable to be output and selects the appropriate stream insertion operator to display the value. The << operator is overloaded to output data items of built-in types integer, float, double, strings and pointer values.
插入运算符 << 可以在单个语句中使用多次,如上所示,而 endl 用于在行的末尾添加新行。
The insertion operator << may be used more than once in a single statement as shown above and endl is used to add a new-line at the end of the line.
The Standard Input Stream (cin)
预定义对象 cin 是 istream 类的实例。cin 对象被认为附加到标准输入设备,它通常是键盘。 cin 与流提取运算符结合使用,它写成 >>,是两个大于号,如以下示例所示。
The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater than signs as shown in the following 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 will prompt you to enter a name. You enter a value and then hit enter to see the following result −
Please enter your name: cplusplus
Your name is: cplusplus
C++ 编译器还确定输入值的数据类型,并选择适当的流提取运算符来提取值并将其存储在给定的变量中。
The C++ compiler also determines the data type of the entered value and selects the appropriate stream extraction operator to extract the value and store it in the given variables.
流提取运算符 >> 可以在单个语句中使用多次。若要请求多个数据项,则可以使用以下内容 −
The stream extraction operator >> may be used more than once in a single statement. To request more than one datum you can use the following −
cin >> name >> age;
这与以下两个语句等效 −
This will be equivalent to the following two statements −
cin >> name;
cin >> age;
The Standard Error Stream (cerr)
预定义对象 cerr 是 ostream 类的实例。cerr 对象被认为附加到标准错误设备,它也是一个显示屏,但对象 cerr 是非缓冲的,对 cerr 的每个流插入都会使输出立即显示。
The predefined object cerr is an instance of ostream class. The cerr object is said to be attached to the standard error device, which is also a display screen but the object cerr is un-buffered and each stream insertion to cerr causes its output to appear immediately.
cerr 也与流插入运算符结合使用,如下例所示。
The cerr is also used in conjunction with the stream insertion operator as shown in the following 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 result −
Error message : Unable to read....
The Standard Log Stream (clog)
预定义对象 clog 是 ostream 类的实例。clog 对象据说已附加到标准错误设备,标准错误设备也是显示屏,但对象 clog 是缓冲的。这意味着每次向 clog 插入数据都可能导致其输出被暂存在缓冲区中,直至缓冲区已满或缓冲区被刷新。
The predefined object clog is an instance of ostream class. The clog object is said to be attached to the standard error device, which is also a display screen but the object clog is buffered. This means that each insertion to clog could cause its output to be held in a buffer until the buffer is filled or until the buffer is flushed.
clog 也与流插入运算符结合使用,如下例所示。
The clog is also used in conjunction with the stream insertion operator as shown in the following 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 result −
Error message : Unable to read....
对于如此小的示例,你可能无法看出 cout、cerr 和 clog 有任何差异,但在编写和执行大型程序时差异会变得很明显。因此,最好使用 cerr 流显示错误消息,而在显示其他日志消息时使用 clog。
You would not be able to see any difference in cout, cerr and clog with these small examples, but while writing and executing big programs the difference becomes obvious. So it is good practice to display error messages using cerr stream and while displaying other log messages then clog should be used.