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 .
A, I / O library header files
Second, the 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. is used cout stream insertion operator << binding
#include <iostream> using namespace std; int main( ) { char str[] = "Hello C++"; cout << "Value of str is : " << str << endl; }
result:
Value of str is : Hello C++
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 << can be used repeatedly in a statement
Third, the standard input stream cin
Cin predefined object is an instance of class iostream. cin objects affiliated to the standard input device, typically a keyboard. extracting cin is used in conjunction operator >>
#include <iostream> a using namespace std; int main () { char name [ 50 ]; cout << " Please enter your name: " ; cin >> name; cout << " Your name is: " << name << endl; }
result:
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
Fourth, the standard error stream cerr
cerr is iostream an instance of a class. Subsidiary objects cerr to the standard error device, usually a display screen, but cerr subject is a non-buffered, and each stream cerr are inserted immediately output .
cerr is inserted into the flow used in conjunction operator <<
#include <iostream> using namespace std; int main( ) { char str[] = "Unable to read...."; cerr << "Error message : " << str << endl; }
result:
Error message : Unable to read....
Fifth, the standard log stream clog
clog is iostream an instance of a class. clog objects affiliated to the standard error device, usually display, but clog the object is buffered. 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 .
clog is inserted into the flow used in conjunction operator <<
#include <iostream> using namespace std; int main( ) { char str[] = "Unable to read...."; clog << "Error message : " << str << endl; }
result:
Error message : Unable to read....
Cerr used to display an error message flow, while the other log messages are used to clog the output stream
Sixth, the input and output stream function