The road to learning C++ primer-(Chapter 17 Input, Output and File Cout)

Speaking of input and output, I should not feel unfamiliar, because the hello world I learned at the beginning is the C++ standard input and output

Simply put, the header file <iostream> is essential (this is the standard input and output header file) cout output instruction function under the namespace std

Example

#include <iostream>
int main ()
{
    using std::cout;
    cout << "Hello World";
    return 0;
}

I need to remind you that the safe use of using mentioned in this book is already there. (Using namespace std is not safe, just define that one if you want).

 

Introduce hex, oct, dec

1.hex means hexadecimal output

You can use cout to operate on it, find the next << to output hexadecimal

int x = 123
cout << hex << x << endl;

result:

7b

2.dec means decimal output

In fact, the cout output is in decimal by default

Example

int x = 123;
cout << x;

3.oct means octal output

int x = 123
cout << oct<< x << endl;

result:

10 

———————————————————— Gorgeous dividing line————————————————————

 cout output and pointer

char name[20] = "Dudly Diddlemore";
cout << name; 
const char *pv = "Violet D'Amore";
cout << " " << pv << "\n";

result:

Dudly Diddlemore Violet D'Amore

Here name prints the first address of the name, cout automatically parses the address of the string or character array into a string for output, so if you want to get the changed string address, you should use (viod *)name to get it after forced conversion address.

 ———————————————————— Gorgeous dividing line————————————————————

Other ostream methods

1.put output a single character

cout.put('W');

Result: W

It returns a cout reference to make splicing calls to it (called chain calls in a language)

cout.put('c').put('+').put('+');

Result: c++

 2.write displays the entire string

The first parameter of write provides the address of the string to be displayed, and the number of characters to be displayed in the second expenditure

const char * pn = "Hello World";
cout.write(pn,4); //显示四个字符

Result: hell

Guess you like

Origin blog.csdn.net/z1455841095/article/details/82843179