C + + Primer Plus Article 17 chapter, Output and file

Source: Internet
Author: User
Tags control characters

Article 17 chapter input, output and documentation

1. When the input sentence arrives. He will flush out the output buffer full output current

2.STREAMBUF Classification It provides a buffer for various operations
The Ios_base class represents the general characteristics of a stream
iOS-based class ios_base. It includes a pointer to a point Streambuf object
The Ostream class derives from iOS. Provides an output method
The IStream class derives from iOS and provides an input method
The Iostream class inherits the input and output methods based on Ostream and IStream

3. Including iostream, I have actively created 8 stream objects
CIN standard input wcin wide character stream
cout standard output wcout wide character stream
Cerr standard error stream. not be buffered. Send directly to screen wcerr wide character stream
Clog standard error stream. Buffered Wclog processing wchar_t wide character stream

All basic types of 4.c++ P737

5.ostream
Put (), display character
Write (), the display string does not encounter null characters when it stops itself, depending on the length of the parameter. Display characters of this length

Flush buffer: When a newline character is encountered, when the input is going to occur
Force flush Buffer cout<<flush; or flush (cout)

6.cout format input
New changes to floating point numbers
Modern: Floating-point types are displayed as 6-bit. The 0 at the end is not displayed. Numbers are represented by fixed-point notation or scientific notation. When the exponent is greater than or equal to 6 or less than or equal to 5, the scientific notation indicates
Legacy: Floating-point types are displayed with 6-bit decimals, and 0 at the end are not displayed. Numbers are represented by fixed-point notation or scientific notation. When the exponent is greater than or equal to 6 or less than or equal to 5, the scientific notation indicates

The 7.ios_base class stores information describing the state of the narrative format.
Dex Hex Oct Control character. Functions such as Hex (cout) are actually printed with 16 binary
Usually Cout<

int width () returns the current setting
The width of int width (int i) is set to I. and returns the value of the past
The width () method only affects the next item that is displayed, and then restores the default value

The Cout.fill (char) output length does not meet the set length. The extra will be filled with char

Set accuracy: cout.precision (int)

The 8.ios_base class provides the SETF () function to control a variety of formatting features p747-p751

9. Some standard control characters P751 are actually functions

10. header File Iomanip
You can provide frequently used controls, such as
setprecision (int) Setting precision
Setfill (char) to set the fill character
SETW (int) Set field width

Method of use: such as COUT<<SETW (4);

11. Using CIN for input
Cin>>staff_size
The parameters and return values of CIN are used as references. This indicates that the variable can be changed

The CIN and cout objects include a data member describing the state of the narration flow, inherited from the ios_base.
The flow state consists of 3 ios_base elements: Eofbit. Badbit,failbad
Eofbit: When CIN is operating to the end of a file
Failbit: Failed to read to expected character
Badbit: In the case of some failure to diagnose the flow

Some elements and functions of the flow state P756

12. Use Cin>>,cin.get (CH) and cin.get () P761
Principle of Use:
Assuming that you skip whitespace characters, it's easier to use cin>>
Want to check each character, using Cin.get (CH) and cin.get () can only be used to handle line break

13. String input getline (), get (), ignore () P762
IStream & Get (Char*,int,char)
IStream & Get (Char*,int)
IStream & Getline (Char*,int,char)
IStream & Getline (Char*,int)

IStream & cin.ignore (int = 1,int = EOF)
such as Cin.ignore (255, ' \ n ');
The function call reads and discards the next 255 characters or directly to the first line break

The getline assumes that no characters are extracted, and the failbit is set, if the line is drawn with the specified character. But there are other characters in the line, set to Failbit
The get hypothesis is not extracted to any character, then set Failbit

14. Other IStream methods p764-p765
Read () such as Cin.read (gross,144) is similar to get, simply does not return to the input after adding null characters, cannot be a string
Peek () Cin.peek () returns the next character of the input stream, but does not extract the characters from the stream
Gcount ()
Putback () inserts a character into the beginning of the input stream, reading is the first character

15. File input and output
To add a header file FStream

To write to a file
1. Create a Ofstream object
2. Associate the object with a specific file
3. Use the Fout in a way that uses cout

Ofstream Fout;fout.open ("Hjq.txt");

Read file
1. Create a Ifstream object
2. Associate the object with a specific file
3. Use fcin in a way that uses CIN

Last Close Fout.close ()

16. Creating multiple Stream objects creates multiple buffers that are independent of each other
When you open multiple files, you need to create multiple stream objects.

17. File Mode
Describe how the narrative document will be used: Read, write, append, etc.
p773-p781

18. Random Access p783-p788

19. Kernel Format p788-p790
Sstream
Reads formatting information from a String object or writes formatting information to a string object called the kernel format
Ostringstream outstr
Istringstream InStr (Facts) Facts is a Strig object

The last 3 items are too small to use. Check again when applying

17.8 programming exercises have time to do it again
No time expected.


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

C + + Primer Plus Article 17 chapter, Output and file

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.