C ++ primer Study Notes chapter 8 (8/18) Standard Io Library

Source: Internet
Author: User

 

Standard Io Library:

The standard library defines a family of types and supports reading and writing files, control windows, and other devices. It also enables string objects to operate like files. Use inheritance.

 

Stream: a description that transmits data from the source to the target in byte mode.

Input stream: refers to the transmission of bytes from the input device to the memory;

Output stream: refers to the transmission of bytes from the memory to the output device;

 

Header file corresponding to the iostream class library interface:

The header file iostream contains the basic information required by all I/O streams and defines four standard I/O Stream objects:

Cin: standard input stream object (connected to standard input device)

Cout: standard output stream (connected to standard output device)

Cerr: Standard Error stream that is not buffered (display is the default device)

Clog: Buffer standard error stream (printer by default)

 

IOstreamInheritance level:

 

 

 

Currently, the stream class reads and writes a stream composed of Char Types.

 

IO objects cannot be copied or assigned values:

The standard library type cannot be copied or assigned values. Description of this requirement:

First, the stream object cannot be stored in the container type (such as vector), because the container type can only store the element types that support replication.

Second, the form parameter or return type cannot be the stream type. If you want to pass or return an IO object, you must pass or return a pointer or reference to the object.

 

Condition status:

The IO standard library manages a series of condition States to mark whether a given Io object is available.

The stream must be in the error-free status before it can be used for input or output. The simplest way to check whether a stream is available is to check its true value:

If (CIN)

While (CIN> word)

 

The condition status of the IO Standard Library:

STRM: iostate is defined by each iostream class and used to define the condition state.

STRM: badbit STRM: iostate type value, used to indicate the broken stream

STRM: failbit STRM: iostate value, used to indicate failed Io operations

STRM: eofbit STRM: iostate type value, used to indicate that the stream has reached the end of the file

S. EOF () if the eofbit value of stream S is set, this function returns true.

S. Bad () if the badbit value of stream S is set, this function returns true.

S. Fail () if the failbit value of stream S is set, this function returns true.

S. Good () if the stream S is in the valid state, this function returns true

S. Clear () clears all statuses of stream S and sets it to true.

S. Clear (FLAG) is used to set a condition status bit of stream s to true. Flag, type: STRM: iostate

S. setstate (FLAG) adds a specified condition to stream S. Flag, type: STRM: iostate

S. rdstate () returns the current condition of stream S. the return value type is STRM: iostate.

 

Stream status query and control:

Int ival;

While (CIN> ival ,! Cin. EOF () // comma operator. calculate each of its operations, and return the rightmost operations as the result of the entire operation.

{

If (CIN. Bad ())

/* Do something */

If (CIN. Fail ())

{Cerr <"bad data ";

Cin. Clear (istrea: failbit );

Continue;

}

}

 

Manage the output buffer:

Each Io object manages a buffer to store data read and write by the program.

OS <"please ";

The system stores the string literal value in the buffer zone related to the stream OS. In the following situations, the buffer content is refreshed, that is, written to the real output device or file.

(1) The program ends normally. As part of the main return work, all output buffers are cleared.

(2) When there are some uncertainties, the buffer may be full. At this time, the buffer will be refreshed before the next value is written.

(3) refresh the location displayed with the operator, such as Endl, flush, and ends.

(4) After each output operation is completed, use the unitbuf operator to set the internal status of the stream to clear the buffer.

(5) associate the output stream with the input stream. At this time, the output buffer associated with the input stream is refreshed during reading.

 

File input and output:

If the program wants to read and write the control window, use the standard library Defined Object Cin, cout, cerr.

If the program needs to read and write files, it needs to define its own objects and then bind them to the price.

Note: If you need to reuse a file stream to read and write multiple files, you must call clear () to clear the status of the file before closing (). Otherwise, the error status of the last object may remain in the new object.

 

File mode:

In, out, app, ate, trunk, binary

 

Example of using a string stream:

String line, word;

While (Getline (CIN, line ))

{

Istringstream stream (line );

While (Stream> word)

{...}

}

 

File stream Overview

 

Use: file Stream Object> variable 1> variable 2> ...; Complete the read operation;

Use: file stream object <expression 1> Expression 2> ...; Complete the write operation;

# Include <iostream>

# Include <fstream> // contains the file stream header file

Int main ()

{

Ofstream out; // defines the output stream object

Out. Open ("AAA"); // open the file

If (! Out) // failed to open the file. The out value is false.

{

Cout <"cannot open AAA file./N ";

Return 1;

}

Out <"C ++" <89.5 <Endl;

Out <"English" <93.5 <Endl;

Out <"maths" <87 <Endl;

Out. Close (); // close the file

}

Understanding file read/write operations by analogy

Just as cout is the output object of standard read/write operations, the output stream object in file read/write operations is out.

Just as the display destination for standard read/write operations is the display destination, and the display destination for file read/write operations is the file.

Therefore, if it is a file, you must specify the file name in a line of code. Then the out and file names become a set of tools to work with each other, and the data in the memory is written into the file together.

Therefore, at the beginning, we need to define the ofstream object and associate the object with a specific 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.