C ++ input/output stream

Source: Internet
Author: User
Document directory
  • 1. Data Stream Base
  • 2. Set the floating point precision.
  • 3. Set the domain width:

From: http://blog.csdn.net/KevinYuen/archive/2009/04/02/4044790.aspx

 

1. Stream

Low-level I/O functions (such as non-formatted I/O): usually refers to the simple transmission of a large number of bytes from the device to the memory or memory to the device.

Advanced I/O functions (formatting I/O): It combines bytes into meaningful units, such as data types.

For processing large-capacity files, you can use unformatted I/O to obtain the best performance.

2. Get and Getline Functions

The get function without parameter values reads a character (even a space) from the specified stream and returns the character as the call value of the function. EOF is returned when the file ends.

GetThe function can also contain three parameters: the accept type array for receiving characters, array size and separator (default: '/N '). When reading data from the input stream, it is aborted by one character less than the specified maximum number of characters, or the specified Delimiter is terminated. To end the input string in the string array (used by the program as a buffer), null characters are inserted into the character array. Delimiters are not stored in arrays, but are still stored in the input stream (the next character to be read), so unless the delimiters are refreshed from the input stream, otherwise, the result of the second get operation is empty.

GetlineFunction operations are similar to get member functions with three parameters. After reading a string, insert a space in the string array. The difference is that Getline will delete separators from the input stream, instead of placing it in an array.

Iii. Ignore

It is used to skip a specified number of characters (default 1) or go to the specified separator (default EOF). When reading a file, this delimiter can cause ignore to jump to the end of the file to terminate the input.

PutbackThe function is to put the character obtained from the input stream through get back into the input stream. It is useful for applications to scan input streams to find fields starting with a specific character.

PeekThe function is to return a character from the input stream, rather than deleting the character from the input stream.

Iv. Non-formatted input/output of the member functions read, gcount, and write

Read

WriteIt is used to process non-formatted input/output. They are responsible for inputting a certain amount of bytes into the memory array and outputting bytes from the memory array (unformatted ).

GcountThe number of characters read by the last input operation.

V. Stream Control Element

Used to complete the formatting of I/O, such as setting the domain width, setting the precision, setting and clearing the formatting flag, setting the field to fill characters, refreshing the stream, inserting a row in the input stream, and refreshing the stream, insert blank characters in the output stream to skip the blank characters in the input stream.

1. Data Stream Base

Dec Oct hex setbase (for example, setbase (10) decimal)

Decimal octal hexadecimal parameterized stream Operator

2. Set the floating point precision.

Once the precision is set, you can use all the subsequent output stream operations until the accuracy changes.

Cout. Precision (Num );

Setprecision (Num );

3. Set the domain width:

SETW

WidthThe width setting is only used for stream insertion or stream reading in the next row. After this operation is completed, the width is set to 0 (that is, the output value is output according to the Desired width) the width function without parameters returns the current setting.

Vi. Stream format Equipment

The format flag specifies the format type to be executed during the I/O flow operation. The member functions SETF, unsetf, and flags are used to set the control flag.

Example:

Cout. SETF (ISO: showpos) // Add a plus sign and a minus sign before a positive or negative number.

Cout. SETF (ISO: Scientific) // specify the Floating Point Output Using scientific notation

Cout. SETF (ISO: left)

Cout. SETF (ISO: Right) // left/right-aligned output

VII. Stream error status

The bits in the IOS class can be used to test the stream status.

When the file end character is met, the output stream is setEofbitBit. Call the member function to determine whether a file Terminator has been encountered.

When a format error occurs in the streamFailbitBut the characters are not lost. The member function fail determines whether the stream operation fails. This error can be fixed.

When an error occurs that causes data loss, SetDadbit. The member function bad is used to determine whether the stay operation fails. Such a serious error is usually unrecoverable.

If the preceding three states are not set, set goodbit.

A good way to test the stream status is to use the member function.EOF, bad, fail, good.

ClearIt is usually used to restore a stream to "good" to continue the I/O operation on the stream. Because of its default parameter IOs: goodbit, the statement cin. Clear ()

For example, Cin. Clear (IOs: failbit) sets failbit for the stream.

8. Connect the output stream to the input stream

Member FunctionsTieTo synchronize istream and ostream operations to ensure that the output is displayed before the input.

For example: cin. Tie (& cout); but this c ++ will be executed automatically.

Inputstream. Tie (0) removes the connection from the output stream to the input stream

Copy a file: (copy the content in to.txt to from.txt)

View plaincopy to clipboardprint?
Ifstream in ("to.txt ");
Assert (in );
Ofstream out ("from.txt ");
Assert (out );
Out <in. rdbuf ();
Ifstream in ("to.txt ");
Assert (in );
Ofstream out ("from.txt ");
Assert (out );
Out <in. rdbuf ();
 

Strstreams introduction:

Istrstream: istrstream (char * BUF );

// Get a pointer to the zero Terminator array. bytes can be extracted Until zero.

Istrstream: istrstream (char * Buf, int size );

// You can always extract bytes to Buf [size], regardless of whether or not a zero is encountered.

Ostrstream: ostrstream (char *, Int, Int = IOs: Out );

// Char * is the pre-allocated buffer where the character ends

// Int buffer size

// Int = IOs: Out mode. If default, the character is formatted from the beginning of the buffer zone.

One thing to remember about ostrstream is that we have not inserted the zero Terminator that is generally needed at the end of the character array. When we prepare a zero Terminator, use the special operator ends.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/KevinYuen/archive/2009/04/02/4044790.aspx

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.