Continuing from the previous article, if you want to use C++ to write files, you can use ofsteam or fstream types, and complete the three steps of opening, writing data, and closing. Please see the following examples:
#include<fstream>#include<iostream>usingnamespacestd;intmain(){chardata[100]=Welcometodotcpp;ofstreamfile;file.open(d:\dotcpp.dat);file<<data;file.close();return0 ;}
Even if the dotcpp.dat file does not exist in the D drive, running this program will still automatically create and write data to it. The running effect is as follows:
The difference between the operators << and >> that we mainly focus on for file reading and writing is actually the overloaded implementation of the operator. Don’t do it in the opposite direction!