C + + standard IO Library

Source: Internet
Author: User

Overview

Do not hurry to know how to use this thing, let us first look at the C + + standard IO library framework, in fact, quite interesting! So let's get started!

The input and output of C + + is provided by the standard library, which provides a family of types that support reading and writing to devices such as files, string objects, and control windows. On the one hand, these IO types define how to read and write values for built-in types, and on the other hand, users can design their own input and output operations in the same way that the IO Standard library facility reads and writes built-in types.

1. Object-oriented IO library

Object-oriented is a major feature of C + +, and his standard library is no exception, all of them are object-oriented design. The standard library uses inheritance (inheritance) to define a set of object-oriented (object-oriented) classes. The IO type is defined in three separate header files: iostream defines the type of read-write control window, fstream defines the type of read-write named files, and Sstream defines the types of string objects that are stored in memory. Each header file contains the types and their meanings, as well as the inheritance relationships between types such as:

Attention:

    • The stream class described above reads and writes a stream consisting of a char type. In addition, the standard library defines a set of related types that support the wchar_t type. That is, each class name is preceded by a "w" prefix, which distinguishes it from the version of the Char type.
    • IO objects do not allow copying and assignment: So stream objects cannot be stored in vectors or other containers; function parameters and return types cannot be stream types.
2.------condition status defined by the standard IO library

The standard IO Library manages a set of conditional states (condition state) members that are used to mark whether a given IO object is in a usable state, or what particular error is encountered. A set of functions and tags defined by the standard library is listed below, providing the means to access and manipulate the state of the flow.

Strm::iostate This is a machine-related integer alias that represents a type, defined by each IO class, that defines the condition state (STRM represents each flow type here).

A value of type Strm::badbit strm::iostate used to indicate the corrupted stream.

A value of type Strm::failbit strm::iostate that indicates the failed IO operation.

A value of type Strm::eofbit strm::iostate that indicates that the stream has reached the end of the file.

S.eof () Returns the value of the EOF flag bit, true or false.

S.fail () Returns the value of the fail flag bit, true or false.

S.bad () Returns the value of the bad flag bit, true or false.

S.good () detects the validity of the stream s, when the Eof,fail,bad is not true good the flag bit is valid, the call good () function returns the truth.

S.clear () resets all state values in the stream s to a valid state.

S.clear (flag) sets a specified condition state in the stream s to be valid, and flag is the condition state of the strm::iostate type, and all condition states are set to valid if no specific condition state is written in parentheses.

S.setstate (flag) adds the specified condition to the stream, sets the flag bit to the trigger state, and flag is the strm::iostate type object.

S.rdstate () returns the current condition of the stream s, and the return value type is strm::iostate.

Summarize-----Condition Status:
    • All stream objects contain a conditional State member, which is an object of type Strm::iostate and is used in the form of bits.
    • Failbit, Eofbit, Badbit, and goodbit are constant values of four strm::iostate types, each representing a condition state, as shown in the following table:

 

constant meaning fail flag bit OEF flag bit bad label Log bit
ios::badbit input (output) stream fatal error, irreversible 0 0 1 /td>
ios::eofbit reached end of file 0 1 0
ios::failbit input (output) stream non-fatal error, can be redeemed 1 0 0
ios::goodbit flow status completely normal 0 0 0

 

 

    • Badbit, Eofbit, failbit,goodbit constitute four basic flow states.
    • With cout detection goodbit, Badbit, eofbit, failbit values are 0,1,2,4, which is not exactly the same as the above table, hehe (goodbit:0000 0000; badbit:0000 0001;eofbit:0000 0010; failbit:0000 0100).
    • You can use clear and setstate to manipulate and manage conditional state members, which are often associated with badbit,eofbit,failbit,goodbit as well as bits or "|" Use it together.
    • With bad (), fail (), EOF (), and good () operations, you can detect whether a stream state belongs to it, and you can use Rdstate () to return the entire condition State member.
Use of 2.1 rdstate:

The Rdstate member function returns a value of type Iostate, as previously stated, which is a machine-related integer that corresponds to the entire condition state of the stream. The following code: (Excerpt from cplusplus.com)

Getting state of Stream object

#include <iostream>

#include <fstream>

using namespace Std;

int main ()

{

Ifstream is;

Is.open ("test.txt");

if ((Is.rdstate () & ifstream::failbit)! = 0)//detect if the fail flag bit is set, i.e. if an error is encountered

Cerr << "Error opening ' test.txt ' \ n"; return 0;

}

Use of 2.2 clear:

Clear is used to set the flag bit instead of clearing the flag bit, which is to set the flow state according to the arguments, forcing the overwrite of the original state, which works in two ways:

    • With one or more parameters in Failbit, Eofbit, badbit,goodbit, sets the flow state according to the concrete arguments, for example, when the goodbit is the actual parameter. You are 0.
    • Without parameters, which is the default state, the effect is as if with Goodbit as the parameter, all the flag bits are set to valid.

Clearing errors#include <iostream> #include <fstream>using namespace Std;int main () {  char buffer [80] ;  FStream myfile;  Myfile.open ("Test.txt", fstream::in);  MyFile << "Test";  if (Myfile.fail ())  {    cout << "Error writing to test.txt\n";    Myfile.clear ();  }  Myfile.getline (buffer,80);  cout << Buffer << "successfully read from file.\n";  return 0;}
Use of 2.3 setstate:

The setstate is used to set the flag bit state, and the difference between clear is that it only updates the actual parameter corresponding status flag on the basis of the original state, instead of forcing overwriting the original state, using the method:

    • Pass a parameter of type strm::iosatate (can be single or multiple Iostate objects connected with "bit or")
3. The key to managing the output buffers-----is to control how buffers are flushed

Each IO object manages a buffer that stores the data that the program reads and writes. A refresh of the buffer causes the contents to be written to the actual output device or file. The following conditions can cause the buffer to be refreshed:

    • When the program is at normal speed, all output buffers will be emptied.
    • At some indeterminate time, the buffer may be full, in which case the buffer will flush the buffer before the next value is written.
    • Flush the buffer explicitly with the operator (Endl, Ends,flush):

cout<< "hi!" <<endl; Insert line break and flush buffer

cout<< "hi!" <<ends; Insert space character null, and flush buffer

cout<< "hi!" <<flush; Just flush the buffer without inserting any other characters

    • The internal state of the stream is set with the UNITBUF operator, so that the stream empties the buffer after each output operation and restores the system's default management mode with NOUNITBUF.

cout<<unitbuf<< "First" << "second" << "third" <<nounitbuf; Equivalent to cout<< "first" <<flush<< "second" <<flush<< "third" <<flush;

    • Using the tie function to associate an output stream with an input stream, any input operation will first flush the buffer associated with its output stream when the input stream is bound to the output stream.

Supplement------------the use of the tie function:

The tie function can be called by a IStream or Ostream object, using a pointer to a Ostream flow object to do the formal parameter.

    • Cin.tie (&cout);//binding cin and cout together,cin>>ival; will cause the buffer associated with cout to be flushed.
    • Ostream *old_tie=cin.tie (); Returns the currently bound object when no arguments are passed
    • Cin.tie (0); When you accept 0 as a parameter, the current binding is dismissed

This article is only as a basic summary of the C + + standard IO library, the file, the string object, and the specific IO operation method of the control window, will be another author, link as follows:

File input and output in C + +: http://blog.csdn.net/miss_acha/article/details/7194695

C + + standard IO Library

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.