C ++ stream operator (format Control)

Source: Internet
Author: User


No format is specified for input/output data. All input/output data is in the default format. However, you sometimes need to control the data format. In this case, you need to use the format defined in the ios class to control the member functions and call them to complete the format settings.


The format control function of the ios class is as follows:


Long flags () const Returns the current format flag.
Long flays (long newflag) Set the format flag to newflag, and return the old format flag.
Long setf (long bits) Sets the specified format flag to return the old format flag.
Long setf (long bits, long field) Positions the format mark specified by field as bits, and returns the old format mark.
Long unsetf (long bits) Clear the format flag specified by bits and return the old format flag.
Long fill (char c) Set the filling character. By default, it is a space.
Char fill () Returns the current filled character.
Int precision (int val) Set the precision to val to control the valid bit of the output floating point number and return the old value.
Int precision () Returns the old precision value.
Int width (int val) Set the display data width (domain width) to return the original domain width.
Int width () Only the current domain width is returned. The default width is 0. In this case, the insert operation can display data according to the minimum width of the data.

Predefined Operator
When formatting Input and Output Using member functions, each function call needs to write a statement. In particular, it cannot be used in the insert or extract operator expressions, but it uses the operator, you can control the formatting of input and output in the expression of the insert and extract operators. The header file iomanip. h must be embedded in the program.

Dec Input and Output in decimal format
Hex Hexadecimal Input and Output
Oct Octal Input and Output
Ws Extract blank characters
Ends Output a nul character
Endl Output A line feed character and refresh the stream
Flush Refresh stream
Resetiosflags (long) Please remove the specific format flag
Setiosflags (long) Set specific format flag
Setfill (char) Set Filling Characters
Setprecision (int) Set the accuracy of the output floating point number.
Setw (int) Set domain width format Variables

Other stream Functions

Error Handling
An error may occur when I/O operations are performed on a stream object. When an error occurs, the nature of the error is recorded in a data member of the ios class.
Constants that describe the error status defined in the ios class:

Goodbit No error. The normal state eofbit reaches the end of the stream.
Failbit The I/O operation fails. After clearing the status word, you can continue the operation.
Badbit An illegal operation is attempted. After the status word is cleared, the stream may still be available.
Hardfail Fatal and unrecoverable errors.


Member functions of the ostream class
Other member functions of the stream can read characters or strings from the stream, perform input and output operations without formatting on the stream, and directly control the I/O operations on the stream.

Return type Ios class member Description
Ostream * Tie (ostream *) Connects the current stream to the specified output stream. The connected stream is automatically refreshed whenever you need to read the current stream. The C ++ stream library uses cin. tie (cout) to connect the input stream to the output stream. To cancel the connection to the output stream, use is. tie (0)
Ostream * Tie () Returns the pointer to the connection stream.

Return type Ostream class member Description
Ostream & Put (char ch) Outputs A ch to the stream without any conversion.
Ostream & Write (char *, int) Outputs a specified length string to the stream without conversion.
Ostream & Flush () Refresh the stream to output all buffered but not output data.
Ostream & Seekp (streampos) Moves the current pointer of a stream to the given absolute position.
Ostream & Seekp (sereamoff, seek_dir) The current pointer of the stream is similar to the current pointer of the file.
Streampos Teelp () Returns the absolute position of the current pointer of the stream.

Member functions of the istream class

Return type Istream class member Description
Int Get () Read and return a character
Istream & Get (char & c) Read characters and store them in c
Istream & Get (char * ptr, int len, char delim = '') Read the specified character into the buffer until the specified Delimiter is encountered, and the Delimiter is not filled in the buffer.
Istream & Getline (char * ptr, int len, char delim = '') Similar to get (char * ptr, int len, chardelim = ''), but the delimiters are filled in the buffer zone.
Istream & Putback () Put the recently read characters in the backflow
Istream & Read (char *, int) Read the specified length string to the buffer.
Int Peek () Returns the next character in the stream without moving the file pointer.
Istream & Seekg (streampos) Move the current pointer to an absolute address
Istream & Seekg (streampos, seek_dir) Move the current pointer to a relative address
Streampos Tellg () Returns the current pointer.
Istream & Ignore (int n = 1, delim = EOF) The number of characters in an over-current hop, or until a specified Delimiter is encountered.

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.