C ++ Primer Plus study note 11, cprimerplus

Source: Internet
Author: User
Tags setf

C ++ Primer Plus study note 11, cprimerplus
C ++ Primer Plus study note 11

Chapter 4 input, output, and files
<<<
Main content:
1) input and output from the C ++ Angle
2) iostream Series
3) redirection
4) ostream Class Method
5) format the output
6) istream Class Method
7) stream status
8) file I/O
9) use the ifstream class to input data from a file
10) output to a file using the ofstream class
11) use the fstream class for file input and output
12) Command Line Processing
13) binary files
14) random File Access
15) kernel formatting
<<<
1. Use cout for output
1) cout

Cout <value; // prototype: ostream & operator <(int)

2) Other ostream methods:
Put () method -- used to display characters
Prototype: ostream & put (char );

Cout. put ('W'); // display W letters. cout is the object that calls the method, and put () is the class member cout. put ('I '). put ('T'); // concatenate the output // when the prototype is appropriate, you can use the numeric parameter for put (), let the function prototype automatically convert the parameter to the char value cout of steam. put (65); // convert 65 to char and display cout. put (66.3); // convert double value 66.3 to char value 66 and display

3) write () method -- used to display strings
Prototype: basic_ostream <char T, traits> & write (const char_type * s, streamsize n );
The first parameter provides the string address to be displayed, and the second parameter specifies the number of characters to be displayed.
Note: The write method does not automatically stop printing characters when a null character is encountered, but simply prints a specified number of characters, even if it exceeds the string boundary.
4) refresh the output buffer
The Controller flush refreshes the buffer while the Controller endl refreshes the buffer and inserts a line feed symbol.

cout << 'Hello, good-looking! ' << flush;cout << 'Wait just a moment,please. ' << endl;

The Controller is also a function. You can directly use flush () to refresh the cout buffer.

Flush (cout); // or cout <flush;

5) format with cout
The ostream insertion operator converts a value to text format. By default, the format of the value is as follows:
① For char values, a character is displayed in a field with a character in width.
② For a numeric integer, it is displayed in decimal format in a field that just contains this number and a negative number.
③ The string is displayed in a field whose width is equal to the string length.
④ Floating point type changes a bit
New Style: The floating point type is displayed as 6 bits, and the 0 at the end is not displayed.
Vintage: The floating point type is displayed with 6 decimal places, and the 0 at the end is not displayed.
Note that there is no relationship between the number of digits displayed and the precision of the number stored.
⑤ Modify the counting system used for display
Decimal dec, cout default decimal
Hex control operator in hexadecimal format

cout << hex;cout << n;

Octal oct Controller

cout << oct;cout << n;

⑥ Adjust the width of the field

Int width (); // The current int width (int I) of the returned field width; // set the field width to I spaces and return the previous field width value cout. width (12); // remember to align right

Note: The width () method only affects the following items, and the field width is restored to the default value. For example

int w = cout.width(30);cout << w;

Note that when the string is displayed, the field width is set to 30, but when the value of w is displayed, the field width is not 30, because it affects the following projects, so it is displayed as 0
7. Fill in characters

cout.fill('*');

Note that this is different from width, which is always valid.
⑧ Sets the display precision of the floating point.
The default number of digits in C ++ is 6, and 0 at the end is not displayed. The precision () member function enables you to select other values.

Cout. precision (2); // set the precision to 2, and the new precision settings remain valid.

Note: In the old C ++ version, the precision of the default mode is interpreted as the position after the decimal point, and the new mode refers to the total number of digits.

cout.precision(2);20.4 -> 202.78889 -> 2.8

⑨ Print the end 0 and decimal point

Cout. setf (ios_base: showpoint); // indicates the decimal point at the end of the cout display. The default precision is 6-position 20.04-> 20.40 2.8 cout.precision (2);->

⑩ Setf ()
Controls other format options when the decimal point is displayed
Prototype 1,

Fmtflags setf (fmtflags); cout. setf (fmtflags); // This is written in the program

Format constant fmtflags

Ios_base: boolalpha // Input and Output bool, which can be true or falseios_base: showbase // for output, use the C ++ base prefix (0, 0x) ios_base :: showpoint // display the end of the decimal point ios_base: uppercase // For hexadecimal output, use the uppercase letter Eios_base: showpos // Add + to the front of the positive number

Prototype 2,

fmtflags set(fmtflags, fmtflags);

The first parameter contains the fmtflags value to be set, and the second parameter specifies which bits of the first parameter to be cleared.
Setf (long, long) parameters:
Second Parameter
Ios_base: basefield
There are three corresponding parameters: ios_base: dec (base 10) ios_base: oct (base 8) ios_base: hex (base 16)
Second Parameter
Ios_base: floatfield
Corresponding to the first parameter, there are two: ios_base: fixed (using the fixed point counting method) ios_base: scientific (using scientific Notation)
Supplement: The description symbol of printf () in C language. The default C ++ mode corresponds to % g specifiers, and the fixed-point notation corresponds to % f specifiers, the scientific notation corresponds to the % e specifier.
Differences between the three notation
Default Mode: New Mode: The floating point type is displayed as 6 bits, the precision is the total number of BITs, and the 0 at the end is not displayed.
Fixed-point notation and scientific Notation: Precision refers to the number of digits after the decimal point, rather than the total number of digits. The end is 0.
Second Parameter
Ios_base: adjustfield
There are three parameters for the first parameter: ios_base: left (left alignment) ios_base: right (right alignment) ios_base: internal (symbol or base prefix left alignment, value right alignment)
Supplement:
Standard Controller

cout << showpoint << fixed << right;

Iomanip header file # include <iomanip>
The three most commonly used controllers:
Setprecision () sets the precision
Setfill () Fill character
Setw () field width
2. Use cin for Input
Operator function prototype

istream & operator>> (int &);cin >> staff_size;

Cin
1) enter a number
2) accept a string, and the request ends with spaces, tabs, and returns.
Cin. get ()
1) cin. get (ch) is used to receive characters

char ch; cin.get(ch);

2) cin. get (s, n) is used to receive a line of strings. It can receive spaces. Similar to getline, multiple words can be input and separated by spaces.

char a[20]; cin.get(a,20);

Cin. getline ()
Accept a string and receive spaces. Note This and cin (s, n ).

Char m [20] = jklkjilj; cin. getline (m, 5); cout <m <endl; // The output result here is jklk because the last character is '0'

Then we can know that we can directly use cin. getline () has three parameters: receive string to m, receive number, and end character (when omitted, the default value is '\ 0' or'/N ')

char a[20]; cin.getline(a,5);

Getline ()
Receives a string, can receive spaces and output, must contain the header file # include <string>
Note: getline () belongs to the string stream, so we can use it only if the string is defined as the string type. Otherwise, we can only use the cin. getline (m, 20) of the istream stream)
Gets ()
Receives a string, can receive spaces and output, but needs to add the header file # include <string>
In addition, note that it cannot be written as m = gets (); It should be gets (m)
Getchar ()
To receive a character, you must have a header file # include <string>
Note that you cannot write it as getchar (ch); you should write it as ch = getchar (); getchar () is a C-language function, and C ++ is also compatible, but is rarely used.
In summary, according to my habits, C ++ uses char ch when entering characters with spaces. cin. get (ch)
String # include <string> gets (str)
Supplement: the stream is the byte stream in and out of the program. The buffer zone is the temporary storage area in the memory and serves as a bridge between the program and files or other devices.
<<Ends at, January 24 ,.



After reading C Primer Plus, can I learn C ++?

C and C ++ are the same in programming logic, but they are different in programming languages. The advantage of C ++ is that it is object-oriented and easier to use, and more similar to java. If you want to learn CIDR, you should also look at it in combination with a special lecture on CIDR, and then use CIDR to improve your understanding.

An error occurred while learning c primer plus (beyond my understanding)

Scanf ("% f", weight); one less & Symbol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.