C + + standard library IO library (i)

Source: Internet
Author: User

Overview

As with the C language, the language itself does not provide input and output support, and the input and output are done through a standard library. The standard library of C provides a range of functions that can be used to implement input and output, while the C + + standard library provides a range of classes and objects to perform input and output functions, and provides the concept of flow, and the IO classes in the standard library are classes of flow concepts. 80% of the content in the C + + standard library belongs to the STL, and the IO library does not belong to this 80%. The IO Library embodies the idea of object-oriented, but it is possible that the IO class is also based on the template implementation.

IOStreams Basic Concepts

C + + io is done by a stream (an object of a particular class), which refers to a sequence of characters generated in chronological order. In object-oriented thinking, a stream is an object of a specific nature defined by a category (class). The output operation is defined as data flowing into the stream, and the input operation is defined as data outflow stream. The comparison base class is the IStream class and the Ostream class, which represent the input and output streams, respectively.

The global Stream object

The iostream library defines a number of global objects of type IStream and Ostream, which correspond to the standard IO Channel (channels), respectively, Cout,cin,clog,cerr. You can use the redirection mechanism to point different global objects to different locations.

Stream operator

Using the mechanism of operator overloading, the stream class can overload the two shift operators of << and >> into operators responsible for input and output.

Control device

Controls the stream class for certain operations, such as Endl. Assigning Std::endl to the output stream prints ' \ n ', while flush is used to flush the buffer. There are std:ends and so on.

Header file organization and inheritance relationships for standard libraries

The IO library is defined by three header files, each of which is iostream,fstream,sstream. The type of the read-write control window is defined in iostream, and the type that reads and writes the named file is defined in FStream, and the type of the string object in memory is defined in Sstream. Each of the types defined in FStream and Sstream is derived from the related types defined in the iostream header file.

Header file Type

Iostream IStream: Reading from stream

Ostream: Write to the stream

iostream: Read and write for convection

FStream ifstream: Read the file

Ofstream: Writing to a file

FStream: Read and write to a file

Sstream Istringstream: Reading from a String object

Ostringstream: Read String Object write

StringStream: Read and write to a string object

Condition State (flow state)

The input/output stream has a state, and the simplest way to check the state is to check the truth of the state. The stream is in an error state when there is an error in the input or output stream. if (CIN) is false, otherwise if (CIN) is true.

The IO standard library manages a range of function and condition State (condition) members to mark whether a given Io object is in the available state, or what specific errors are encountered. The following is a set of functions and tags defined by the standard library, providing the means to access and manipulate the state of the flow.

Strm:iostate: Machine-related integer names, defined by each iostream class, are used to define the condition state, each iostream class has a State Member of this type, which is used to mark the flow state, which exists in the form of bits. Use clear and setstate for management. Strm:badbit,strm:failbit,strm:eofbit is a value of type strm:iostate that is used to flag specific content. The standard library also provides a series of functions to manage the state of the flow, returning different results depending on the condition state, or setting the flow state. such as s.eof (), S.fail (), S.bad (), S.good (), etc. An example of using a State class:

int ival;

while (Cin>>ival,!cin.eof ())

{

if (Cin.bad ())

Throw Runtime_error ("IO stream corrupted");

if (Cin.fail ())

{

cerr<< "Bad Data,try again";

Cin.clear (Istream:failbit);

Continue

}

}

Management of output buffers

Each IO object manages a buffer that stores the data that the program reads and writes to. A buffer being refreshed means that the contents of the buffer are written to the actual output device or file. There are 5 ways to flush buffers, two default passive ways, and three active ways. The default method is when the program exits normally, and when the buffer is full. The active way is to use the operator, such as Endl,ends,flush. Starting with Unitbuf, the output is refreshed with each output, ending with nounitbuf. This method is equivalent to setting the state of the stream. The third initiative is to bind the input and output stream using the tie. cout and Cin are bound. This causes the cout buffer to be flushed each time it is entered. Establish binding and Unbind by: Cin.tie (&cout), Cin.tie (0). A binding function is a member function of a stream.

C + + standard library IO library (i)

Related Article

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.