C + + Primer Fourth Edition reading notes (vii) Standard IO Library

Source: Internet
Author: User

The input/output of C + + is provided by the standard library. The standard library defines a set of types that support read-write (IO) for devices such as files and control Windows. Other types have also been defined so that the string object can operate like a file, allowing us to convert between data and characters without the need for IO. In general, class designers can also easily use the IO standard library facility to read and write objects of custom classes. Class types typically use the IO standard library to read and write operators and rules defined for built-in types.

I. Object-oriented standard library

So far, we have used IO types and objects to read and write data streams, which are often used to interact with user-controlled windows. Of course, the actual program can not be limited to the IO of the control window, usually also need to read or write the named file. In addition, the program should be able to easily use IO operations to format the data in memory, thus avoiding the complexity and operational costs of reading and writing disks or other devices. Applications also need to support reading and writing in wide-character languages.

In general, types that are associated by inheritance share common interfaces. When a class inherits from another class, these two classes can usually use the same operation. Rather, if there is an inheritance relationship between the two types, it can be said that a class "inherits" the behavior of its parent class-the interface.

The IO type is defined in three separate header files: iostream defines the type of the read-write control window, fstream defines the type of read-write named files, and the type defined by Sstream is used to read and write the string objects stored in memory. Each of the types defined in FStream and Sstream is derived from the related types defined in the iostream header file.

There is another important implication of using inheritance for IO types: If a function has a reference parameter to a base class type, you can pass an object of its derived type to the function. This means that functions that operate on istream& can also be invoked using Ifstream or Istringstream objects.

1.1 Support for international characters

Each IO header file defines classes and standard input/output objects of type char and wchar_t. Classes and objects based on the wchar_t type of the stream are defined in iostream, the wide character file stream type is defined in FStream, and the wide character stringstream is defined in the Sstream header file.

1.2 IO objects cannot be copied or assigned

In general, if you are passing an IO object to read and write to it, you can pass the stream object in a way that is not a const reference. Reading and writing to an Io object alters its state, so the reference must be non-Const.

Second, condition status

Implementation of IO Inheritance formal error occurs at the root. Some errors are recoverable, and some errors occur at the bottom of the system and are outside the scope of the program's correction. The IO standard library manages some column condition state members to mark whether a given Io object is in a usable state, or what particular error is encountered.

condition Status of the IO standard library
Function Mark
Strm::iostate Machine-related integer names, defined by individual iostream classes, to define condition states
Strm::badbit A value of type strm::iostate that indicates the corrupted stream
Strm::failbit A value of type Strm::iostate that indicates the failed IO operation
Strm::eofbit A value of type strm::iostate that indicates that the stream has reached the file terminator
S.eof () If the eofbit value of the stream S is set, the function returns True
S.fail () If the failbit value of the stream S is set, the function returns True
S.bad () If the badbit value of the stream S is set, the function returns True
S.good () If stream s is in a valid state, the function returns True
S.clear () Resets all state values in stream s to a valid state
S.clear (flag) Sets a specified condition state in stream s to valid. The type of flag is strm::iostate
S.setstate (flag) Adds the specified condition to the stream s. The type of flag is strm::iostate
S.rdstate () Returns the current condition of stream s, the return value type is strm::iostate

2.1 Item Status

All stream objects contain a conditional State member, which is managed by the SetState and clear operations.

2.2 Flow status Query and control

Third, the management of the output buffer

3.1 Refresh of output buffers

Endl: Used to output a newline character and flush the buffer

Flush: Used to refresh the stream, but not to price any characters in the output

Ends: Less use, insert a null character in the buffer null, and then refresh it

3.2 Unitbuf operator

If there is output to refresh, it is best to use the UNITBUF operator. This operator flushes the stream after each execution.

3.3 binding inputs and outputs together

Interactive systems should generally ensure that their input and output streams are bound together. This means that any output, including prompts to the user, is guaranteed to be output before attempting to read.

Iv. file input and output

The FStream header file defines the three types of support file IO:

(1) Ifstream, derived from IStream, provides the function of reading documents;

(2) Ofstream, derived from Osteam, provides the function of writing files;

(3) FStream, derived from iostream, provides the ability to read and write the same file.

4.1 Use of File stream objects

When you need to read and write files, you must define your own objects and bind them to the required files.

Before you use the FStream object, you must also bind the objects to read and write files.

Call the open member function to bind an existing FStream object to a specific file. In order to read and write, the specified file needs to be opened and positioned, and the open function completes all required operations specified by the system.

4.1.1 Check if File Open is successful

After opening a file, it is often a good practice to verify that the open is successful.

Ifstream infile;

Infile.open ("in");

if (!infile)

{

...........................

return-1;

}

4.1.2 to re-bundle a file stream with a new file

Once the FStream object is open, it remains associated with the specified file. If you want to associate a FStream object with a different file, you must close the existing file (close) and open another file (open).

4.1.3 purging the state of a file stream

If a programmer needs to read and write multiple files with a file stream, clear clears the state of the stream before reading another file.

4.2 File Mode

When you open a file, you need to specify a file mode whether to call open or a file name as part of the stream initialization. Each FStream class defines a set of values that represent different patterns that specify the different patterns that the stream opens. As with the condition status identifier, the file mode is also an integer constant. Both the file stream constructor and the open function provide the default argument settings file mode.

File mode
Inch Open file for read operation
Out Open file for write operation
App Find the end of the file before each write
Ate Locate the file at the end of the file immediately after opening it
Trunc Empty the existing file stream when opening the file
Binary IO operation in binary mode

Out, Trunc, and app modes can only be used to specify files associated with ofstream or FStream objects; In mode can only be used to specify files associated with Ifstream or FStream objects. All files can be opened using ate or binary mode. Ate mode is only valid when open: The file will be located at the end of the file when it is opened. A stream opened in binary mode processes the file as a sequence of bytes without interpreting the characters in the stream.

By default, the file associated with the Ifstream stream object is opened in mode, which allows the file to read, and the file associated with the iostream is opened in the Out mode, which is the file writable. Files opened in out mode are emptied: All data stored by the file is discarded.

Note: From the effect, performing an out mode for a Ofstream object is equivalent to specifying both out and TRUNC modes.

4.2.1 output and input operations on the same file

By default, the FStream object is opened in and out mode at the same time. does not empty when the file is opened with both in and out. If the file associated with FStream is opened, only the out mode is used and no in mode is specified, the file empties the existing data. If Trunc mode is specified when the file is opened, the file is also emptied, regardless of whether in mode is specified at the same time.

4.2.2 Mode is a property of a file rather than a stream

Note: Whenever you call the Open function, set the file mode, which can be either explicit or implicit. If you do not specify a file mode, the default value is used.

4.2.3 Effective combination of open mode

Not all open modes can be specified at the same time. There are some patterns that don't make sense:

Combination of file modes
Out Open file for write operation, delete data from file
Out | App Open file for write operation, write at end of file
Out | Trunc Same as Out mode
Inch Open file for read operation
In | Out Open a file for read and write operations, and locate it at the beginning of the file
In | Out | Trunc Open the file for read and write operations, delete the data already in the file

4.3 A program to open and check the input file

V. String flow


C + + Primer Fourth Edition reading notes (vii) 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.