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

Program input refers to the transfer of data from the input file to the program, and program output refers to the transfer of data from the program to the output file.
C++ input and output includes the following three aspects:
input and output to the 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.
The external storage disk file is the object for input and output, that is, the 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.
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.

The development of C++ I/O to C-type safety and scalability.
In C language, using printf and scanf for input and output often cannot guarantee that the input and output data is reliable and safe. In the input and output of C++, the compilation 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.
C++ implements rich I/O functions through I/O libraries. This makes the C++ input and output significantly better than printf and scanf in the C language, but it also pays for it. The C++ I/O system becomes more complicated, and many details need to be mastered.
The C++ compilation system provides an iostream class library for input and output. The word iostream is composed of 3 parts, namely io-stream, which means input and output stream. In the iostream class library contains many classes for input and output. Commonly used in the table,
Insert picture description here
Insert picture description here
ios is an abstract base class from which istream class and ostream class are derived. The first letters i and o in the two class names represent input and output respectively. The istream class supports input operations, the ostream class supports output operations, and the iostream class supports input and output operations. The iostream class is a class derived from the istream class and the ostream class through multiple inheritance. The inheritance hierarchy is shown in the figure above.
C++ needs to use ifstrcam and ofstream classes for file input and output. In the two class names, the first letter i and o represent input and output respectively, and the second letter f represents file. ifstream supports input operations on files, ofstream supports output operations on files. The class ifstream inherits the class istream, the class ofstream inherits the class ostream, and the class fstream inherits the class iostream. See picture
Insert picture description here
There are other classes in the I/O class library, but for the average user, the above can meet the needs.
The header files related to the
iostream class 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 own program, which is equivalent to declaring in this program The classes that need to be used. It can be another way of saying: 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.
Stream objects 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.

The iostream header file not only defines related classes, but also defines 4 kinds of stream objects. The
meaning of the objects corresponds to the corresponding standard file in the C-like language corresponding to the device
cin standard input stream keyboard istream_withassign stdin
cout standard output stream screen ostream_withassign stdout
cerr standard Error stream screen ostream_withassign stderr
clog Standard error stream screen ostream_withassign stderr

Define the above four stream objects in the iostream header file in the following form (taking cout as an example):
    ostream cout (stdout);
When defining cout as an ostream stream object, use the standard output device stdout as a parameter, so that it is the same as The standard output device (display) is connected, and if there is
    cout <<3;
it will output 3 on the display screen.

The overloaded operators
"<<" and ">>" in the iostream header file were originally defined as the left shift operator and the right shift operator in C++. Because they are overloaded in the iostream header file, They can be used as input and output operators for standard types of data. Therefore, the #include command must be used to include the iostream in the program using them.
    #include
1)>>a means to put the data in the a object.
2) <<a means to take out the data stored in the a object.

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/109214963