For the first two steps, after everyone is familiar with opening, reading and writing, the last step of closing the file will be much easier. After completing the relevant operations, you only need to call the close() member function at the end, which will disconnect the file from the program and end the operation.
The close function is a member function of ifstream , ofstream , and fstream . When used, it can be called directly through . with the open file object .
By studying the previous read and write operations, you can pay attention to the statement of closing the file in the last step, such as:
#include<fstream>#include<iostream>usingnamespacestd;intmain(){chardata[100]=Welcometodotcpp;ofstreamfile;file.open(d:\dotcpp.dat);file<<data;file.close();/ /Call the close function to close the file operation return0;}