Having created a stream with the declaration above, we can connect it to a file (i.e. open the file) using the member function open(filename). For example, the following statement will allow the C++ program to open the file called “myFile.txt”, assuming a file named that exists in the current directory, and connect in_stream to the beginning of the file:
Because out_stream is an object of type <ofstream>, connecting it to the file named “anotherFile.txt” will create that file if it does not exist. If the file “anotherFile.txt” already exists, it will be wiped and replaced with whatever is fed into the output stream.
Say you wanted to add some text to a file that already has important information on it. Would it be a good idea to first use ofstream to open the file?