Poco C + + library learning and analysis-stream (i)

Source: Internet
Author: User
Tags locale setf

Stream is a big difference between C + + and. Programmers who write C + + know the usage of streams. In the Poco library, a number of streams have been added to the standard stream, namely Base64 and hexbinary based codec streams, data compression streams using zlib, binary I/O streams, file streams, and some other auxiliary streams, and the Poco library provides an extended structure Used to create a user-defined stream.

All stream classes in the Poco library are compatible with streams in the standard C + + library. And in Poco libraries, most streams are just filters, which means they don't read or write data directly from the device, and typically they link to another stream. Let's introduce them separately.

1. Introduction to standard C + + flow

Before you introduce Poco streams, it is necessary to understand the input and output streams in C + +, or you will find the streams in Poco difficult to understand. After reading the C + + flow structure, you will naturally be enlightened about the flow content in the Poco library.

To ensure language and platform independence, C + + and C do not have internal input and output capabilities. The input and output capabilities of the language are related to the operating system and are implemented at the lowest level by invoking the OS's I/O library.

In the iostream stream Library of C + +, there are two basic parts. respectively:

1. Stream: C + + regards input and output as byte stream. When entered, the program extracts bytes from the input stream, and when output, the program inserts the bytes into the output stream. A stream acts as a bridge between a program and a flow source or a flow target.

2. Buffer: A buffer is a block of memory used as a mediator, which is a temporary storage tool that transfers information from the device to the program or from the program to the device to match the speed gap between the program and the device. In design, the addition of buffers makes the iostream structure of C + + more extensible.

C + + input and Output class diagram:

 

1.1, Ios_base

The Ios_base class encapsulates the flow in the C + + standard and defines the basic information and behavior of data types that do not depend on read and write in the input and output, such as formatting information, exception states, event callbacks, and so on.

In class Std::ios_base, the following information about the stream is saved:

* Format Control Information enumeration type Fmtflags, affect how to interpret the input serial format, how to generate output serial format, such as Integer is 16 or 10 binary notation, floating-point number is the use of scientific and technical law or fixed-point form;

* Stream State enumeration type Iostate, such as whether the data is complete, whether to reach the end of the stream, whether read failure, etc.;

* Stream Open Mode enumeration type OpenMode, such as read, write, append, create delete original content, binary open;

* Position Enumeration type Seekdir of the stream, such as start position, current position, end position, etc.;

* Stream Event enumeration type event, such as "erase" event erase_event, change locale settings event imbue_event, copy format events copyfmt_event;

* Stream private additional saved data for a long array with an array of pointers;

* A member class failure, used as a base class for various exceptions thrown by the stream input and output class library as a C + + standard;

* A member of INIT, used to encapsulate initialization functions for 8 static objects such as cout, CIN, wcout, etc.

  member functions include:

  Formatting:

1. Read/Set the format of the stream

* Fmtflags flags () const;

* Fmtflags flags (fmtflags FMTFL);

Example:

1#include <iostream>2 3 intMain ()4 {5Std::cout.flags (std::ios::right | std::ios::hex |std::ios::showbase);6Std::cout.width (Ten);7Std::cout << -<<'\ n';8     return 0;9 }Ten  OneOutput results: A          0x64

2. Format the stream and merge it with the original format

* Fmtflags setf (fmtflags FMTFL);

* Fmtflags setf (fmtflags FMTFL, fmtflags mask);

Example:

1#include <iostream>2 3 intMain ()4 {5STD::COUT.SETF (Std::ios::hex, Std::ios::basefield);//set hex as the Basefield6STD::COUT.SETF (std::ios::showbase);//Activate Showbase7Std::cout << -<<'\ n';8STD::COUT.UNSETF (std::ios::showbase);//Deactivate Showbase9Std::cout << -<<'\ n';Ten     return 0; One } A  - Output Result: - 0x64 the  -

3. According to the parameter mask, clear the format of some bits of the stream (bit)

* void Unsetf (fmtflags mask);

Example:

1#include <iostream>2 3 intMain ()4 {5STD::COUT.SETF (Std::ios::hex, Std::ios::basefield);//set hex as the Basefield6STD::COUT.SETF (std::ios::showbase);//Activate Showbase7Std::cout << -<<'\ n';8STD::COUT.UNSETF (std::ios::showbase);//Deactivate Showbase9Std::cout << -<<'\ n';Ten     return 0; One } A  - Output Result: - 0x64 the  -

4. Read/Set the accuracy when displaying floating-point numbers

* Streamsize precision () const;

* Streamsize Precision (streamsize prec);

Example:

1#include <iostream>2 3 intMain ()4 {5     Doublef =3.14159;6STD::COUT.UNSETF (Std::ios::floatfield);//Floatfield not set7Std::cout.precision (5);8Std::cout << F <<'\ n';9Std::cout.precision (Ten);TenStd::cout << F <<'\ n'; OneSTD::COUT.SETF (Std::ios::fixed, Std::ios::floatfield);//Floatfield set to fixed AStd::cout << F <<'\ n'; -     return 0; - } the  -  - Output Result: - 3.1416 + 3.14159 - 3.1415900000

5. Read/Set the display width of the output data of the stream

* Streamsize width () const;

* Streamsize width (streamsize wide);

Example:

1#include <iostream>2 3 intMain ()4 {5Std::cout << -<<'\ n';6Std::cout.width (Ten);7Std::cout << -<<'\ n';8Std::cout.fill ('x');9Std::cout.width ( the);TenStd::cout << Std::left << -<<'\ n'; One     return 0; A } -  - Output Result: the  - -         - -100xxxxxxxxxxxx

 Language environment:

1. Set the local locale for the stream

* Locale Imbue (const locale& LOC);

Example:

1#include <iostream>2#include <locale>3 4 intMain ()5 {6Std::locale Mylocale ("");//Get global locale7Std::cout.imbue (Mylocale);//Imbue Global Locale8Std::cout <<3.14159<<'\ n';9     return 0;Ten } One  A Output Result: - 3.14159

2. Get the currently used locale

* Locale getloc () const;

1.2 Basic_ios 

Basic_ios defines the common properties of the stream class associated with character types and their corresponding character attributes, including flow status, setting flow status, copying flow flags, returning or setting a stream buffer pointer, setting localization-related information, returning or setting padding characters, character conversions, The buffer period used by stre AM is also included.

Basic_ios defines a pointer to streambuf within it.

template<classclass traits>class  basic_ios        :public  iOS The       _base{//C + + standard library encapsulates a buffer class streambuf for input and output stream objects        using // per standard C + + The input and output stream object contains a pointer to Streambuf       Basic_streambuf<_elem, _traits> * mystrbuf;        //  ....   };    

member functions include:

1. Status Tag function:

* BOOL Good () const; Check if the stream status is good

* BOOL EOF () const; Check whether the stream status bit is Eof,eofbit bit marked

* BOOL Fail () const; Check whether the stream status bit is Fail,failbit or badbit is flagged

* BOOL Bad () const; Check whether the stream status is bad,badbit bit marked

* Iostate rdstate () const; Return stream Status bit

There are two ways to get status information for input/output. One method is to return the error token of the current state by calling the Rdstate () function. Another method is to use the good (), EOF (), fail (), bad () function to check the corresponding input/output state.

Poco C + + library learning and analysis-stream (i)

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.