The C++ standard library provides streams for exchanging data with files. For writing to a file the class ofstream
(output file stream) is used. To write to a file, it must first be opened with a file name. Preparations for writing are made during opening. Opening a file can be done right at the instantiation with the constructor or with the help of the element function open()
.
At the end of the write operations, at the latest when the process is terminated, the file must be closed again.
The closing can be done explicitly with the element function close()
or implicitly by the destructor of the output stream. For reading from a file the class ifstream
(input file stream) is used. The class fstream
(file stream) is used for both – reading and writing from and to a file.