Latest web development tutorials

C ++ Basic Input Output

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

The C ++ I / O occurs in the stream, the stream is a sequence of bytes. If the byte stream is flowing from the device (such as a keyboard, disk drives, network connections, etc.) memory, which is called theinput operation.If the byte stream flowing from the memory devices (such as displays, printers, disk drives, network connections, etc.), which is calledthe output operation.

I / O library header files

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

头文件函数和描述
<iostream>该文件定义了cin、cout、cerrclog对象,分别对应于标准输入流、标准输出流、非缓冲标准错误流和缓冲标准错误流。
<iomanip>该文件通过所谓的参数化的流操纵器(比如setwsetprecision),来声明对执行标准化 I/O 有用的服务。
<fstream>该文件为用户控制的文件处理声明服务。我们将在文件和流的相关章节讨论它的细节。

The standard output stream (cout)

Predefined objectcout is an instance of the ostreamclass. cout object is "connected" to the standard output device, usually the display.cout << operator is used in conjunction with the stream insertion as follows:

#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++

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

Stream insertion operator << can be used multiple times in a statement, as shown in the aboveexamples, endl for the end of the line to add a line break.

Standard input stream (cin)

Predefined objectscin istreamis an instance of the class. cin object is affiliated to the standard input device, usually the keyboard.cin extraction is stream operator >> used in combination, 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 to enter a name. When the user enters a value and press the Enter key, you will see the following results:

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

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

Stream extraction operator >> can be used multiple times in a statement, if a plurality of data input requirements, 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)

Predefined objectostream cerris an instance of the class. cerr object is affiliated to the standard error device, usually the display, but the object is non-bufferedcerr, and each stream cerr is immediately inserted into the output.

cerr also stream insertion operator << and used in combination, 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....

Standard log stream (clog)

Predefined objectsclog is an instance of the ostreamclass. clog objects affiliated to the standard error device, usually the display, but the object is bufferedclog.This means that each stream into the clog will be stored in the buffer in until the buffer fills, or only when the output buffer is flushed.

alsoclog the stream insertion operator << used in combination, 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....

Through these small examples, we can not distinguish cout, cerr and clog the difference, but in the preparation and implementation of large programs, the difference between them becomes very apparent. So good programming practice tells us that use cerr stream to display an error message, while others use the log messages clog stream output.