A file stream is a stream of data that is an input and output object as an external memory file. The output file stream is the data that flows from memory to the external memory file, and the input file stream is the data that flows from the external memory file to memory. Each file stream has a memory buffer corresponding to it.
Differentiate between file stream and file concepts without mistaking the flow of files as a stream of several files. The file stream itself is not a file, but a stream of input and output objects as a file. To input output to a disk file, you must do so through a file stream.
Several file classes are defined in the C + + I/O class library, specifically for input and output operations on disk files. In Figure 13.2 (see more: classes and objects related to C + + input and output ), there are 3 file classes for file operations in addition to the standard input and output stream classes IStream, Ostream, and iostream classes:
- The Ifstream class, which is derived from the IStream class, is used to support input from a disk file.
- The Ofstream class, which is derived from the Ostream class, is used to support output to disk files.
- The FStream class, which is derived from the Iostream class, is used to support input and output to disk files.
More Knowledge content
To input and output a disk file for an object, you must define an object for the file stream class, output the data from memory to a disk file through a file stream object, or enter the data into memory from a file stream object from a disk file.
In fact, in the input and output of the standard device object, but also to define the flow object, such as CIN, cout is the stream object, C + + is through the stream object input and output. Since Cin and cout are already defined in the iostream.h, the user does not need to define them. When disk files are used, they cannot be defined in advance because of different circumstances, and must be defined by the user. In addition, operations on disk files are implemented through file stream objects rather than CIN and cout. File stream objects are defined by the file stream class, not by the IStream and Ostream classes. You can create an output file stream object in the following ways:
Ofstream outfile;
As with the flow object cout-defined in the header file iostream, an object outfile to the Ofstream class (output file stream Class) is now defined in the program. But there is a question that has not been resolved: when defining cout, it is associated with a standard output device (monitor), and now, although an output file stream object is established, it is not specified to which disk file it will be exported, and it needs to be specified when it is used. The next section is about to answer this question.
C + + file stream class and file stream object specific description