Preface/PREFACE
The standard C++ file operations are mainly accomplished through the file stream fstream. File input/output streams are used to implement file reading and storage operations, as illustrated in the following diagram:

How to read from a file input stream and write to a file output stream. This requires the use of another standard library in C++, fstream, which defines three new data types:
a) ofstream: This data type represents an output file stream, used to create files and write information to them.
b) ifstream: This data type represents an input file stream, used to read information from files.
c) fstream: This data type typically represents a file stream and has the functions of both ofstream and ifstream, meaning it can create files, write information to files, and read information from files.
The inheritance relationship is illustrated in the following diagram:

➤ Open File
Before reading information from a file or writing information to a file, you must first open the file. ofstream and fstream objects can be used to open files for writing, while ifstream objects are used to open files for reading.
The following is the standard syntax for the open() function, which is a member of fstream, ifstream, and ofstream objects.

Here, the first parameter of the open() member function specifies the name and location of the file to be opened, and the second parameter defines the mode in which the file is opened.

➤ Close File
When a C++ program terminates, it automatically closes and flushes all streams, releases all allocated memory, and closes all opened files. However, programmers should develop the good habit of closing all opened files before program termination.
The following is the standard syntax for the close() function, which is a member of fstream, ifstream, and ofstream objects.

➤ Write to File
In C++ programming, we use the stream insertion operator ( << ) to write information to a file, just as we use this operator to output information to the screen. The only difference is that here you are using ofstream or fstream objects instead of the cout object.
➤ Read from File
In C++ programming, we use the stream extraction operator ( >> ) to read information from a file, just as we use this operator to input information from the keyboard. The only difference is that here you are using ifstream or fstream objects instead of the cin object.
➤ Read & Write Example
The following C++ program opens a file in read/write mode. After writing user input information to the file afile.dat, the program reads information from the file and outputs it to the screen:

When the above code is compiled and executed, it produces the following input and output:

(Note: In the above example, additional functions of the cin object are used, such as the getline() function to read a line from external input, and the ignore() function to ignore excess characters left by previous read statements).
➤ File Position Pointer
istream and ostream both provide member functions for repositioning the file position pointer. These member functions include seekg (“seek get”) for istream and seekp (“seek put”) for ostream.
seekg and seekp parameters are usually a long integer. The second parameter can be used to specify the direction of the search. The search direction can be ios::beg (default, starting from the beginning of the stream), ios::cur (starting from the current position in the stream), or ios::end (starting from the end of the stream).
The file position pointer is an integer value that specifies the number of bytes from the start of the file to the current pointer position. Below is an example of locating the “get” file position pointer:


REC

May all that is desired be obtained,
And may all that is lost be inconsequential.

*Statement:This article is compiled from the internet, and the copyright belongs to the original author. If the source information is incorrect or infringes rights, please contact us for deletion or authorization matters.