C++: The concept of I/O stream and the structure of stream library

1. C++ input and output include the following three aspects:

  1. Input and output of standard equipment specified by the system. That is, input data from the keyboard and output to the monitor screen. This input and output is called standard input and output, or standard I/O for short .
  2. The external storage disk file is the object for input and output, that is, data is input from the disk file and the data is output to the disk file. The input and output of external storage files is called file input and output, or file I/O for short .
  3. Input and output to the specified space in the memory. Usually specify a character array as storage space (in fact, you can use this space to store any information). This input and output is called string input and output, or string I/O for short .

2. The development of C++ I/O to C-type safety and scalability

In the input and output of C++, the compiler system strictly checks the data type, and any data with an incorrect type cannot be compiled. Therefore, C++ I/O operations are type safe.

The C++ I/O operation is extensible, not only can be used to input and output standard types of data, but also can be used for user-defined types of data.
Insert picture description here
Class inheritance relationship:
Insert picture description here

Three, the header files related to the iostream library

The declarations of different classes in the iostream class library are placed in different header files. The user uses the #include command to include the relevant header files in his program, which is equivalent to declaring the required classes in the program. To put it another way: the header file is the interface between the program and the class library, and the interface of the iostream class library is implemented by different header files. Commonly used

  • iostream contains the basic information needed to operate on input and output streams.
  • fstream is used for I/O operations of user-managed files.
  • strstream is used for string stream I/O.
  • stdiostream is used when mixing C and C++ I/O mechanisms, for example, when you want to convert a C program into a C++ program.
  • iomanip should include this header file when using formatted I/O.

Fourth, the stream object defined in the iostream header file

The classes defined in the iostream header file are ios, istream, ostream, iostream, istream_withassign, ostream_withassign, iostream_withassign, etc.

In the iostream header file, not only the related classes are defined, but also 4 kinds of stream objects are defined:

Object meaning Corresponding equipment Corresponding class Corresponding standard documents in c language
China Standard input stream keyboard istream_withassign stdin
cost Standard output stream screen ostream_withassign stdout
cerr Standard error stream screen ostream_withassign stderr
clog Standard error stream screen ostream_withassign stderr

Five, overload operators in the iostream header file

"<<" and ">>" were originally defined as left shift operator and right shift operator in C++. Because they are overloaded in the iostream header file, they can be used as standard type data Input and output operators. Therefore, you must use the #include command to include the iostream in the program that uses them.

#include <iostream>

  1. >>a means to put data into the a object.
  2. <<a means to take out the data stored in the a object.

Guess you like

Origin blog.csdn.net/weixin_45341339/article/details/112967869