Wu Yuxiong - born natural C ++ language study notes: C ++ Basic Input Output

The C ++ I / O occurs in the stream, the stream is a sequence of bytes. If the flow stream of bytes from the memory device (e.g., keyboard, disk drive, network connection, etc.), which is called the input operation. If the byte stream flowing from the memory device (e.g., display screen, printer, disk drive, network connection, etc.), which is called the output operation.
The following header files in C ++ is very important in programming.
<iostream>     This file defines cin, cout, cerr and clog objects, respectively corresponding to the standard input stream, standard output and standard error unbuffered and buffered standard error.
<iomanip> the file by the so-called parametric flow manipulator (such setw and setprecision), normalized to declare performs I / Useful O services.
<fstream> file to the user control file processing declaration services.
Standard output stream (COUT)
Cout predefined object is an instance of class iostream. cout Object " connected " to the standard output device, usually a display screen. cout is a stream insertion operator << used in combination, as follows:
#include <iostream>
 
using namespace std;
 
int main ()
{
   char str[] = "Hello C++";
 
   cout << "Value of str is : " << str << endl;
}
C ++ compiler output variable according to the type of data to choose the appropriate stream insertion operator to display the value. << operator is overloaded output built-in type (integer, float, Double , string and pointer) data items.
Stream insertion operator << be used multiple times within a single statement, as shown in the above example, for the end of the line to add endl a newline.
Standard input stream (CIN)
Cin predefined object is an instance of class iostream. cin objects affiliated to the standard input device, typically a keyboard. cin is extracting operator >> used in combination, as follows:
#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 the user to enter a name. When the user enters a value, press the Enter key, you will see the following results:
Enter your name: cplusplus
Your name: cplusplus
C ++ compiler according to the data type of the input values to select the appropriate stream extraction operator to extract value, and stores it in a given variable.
Extracting operator >> can be used repeatedly in a statement, if a plurality of data input requirements, the following statement may be used:
cin >> name >> age;
This is equivalent to the following two statements:
cin >> name;
cin >> age;
The standard error stream (cerr)
Cerr predefined object is an instance of class iostream. Subsidiary objects cerr to the standard error device, usually a display screen, but the object is non-buffered cerr, and each stream cerr are inserted immediately output.
cerr is flowing insertion operator << used in combination, as follows:
#include <iostream>
 
using namespace std;
 
int main ()
{
   char str[] = "Unable to read....";
 
   cerr << "Error message : " << str << endl;
}
Standard log stream (the clog)
Clog predefined object is an instance of class iostream. clog objects affiliated to the standard error device, usually display, but the object is buffered clog. This means that each flow clog will be inserted into the first stored in the buffer, only the output buffer until the buffer fills or refresh.
also clog the flow insertion operator << used in combination, as follows:
#include <iostream>
 
using namespace std;
 
int main ()
{
   char str[] = "Unable to read....";
 
   clog << "Error message : " << str << endl;
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/12148745.html