It is common practice to open a file for writing in C++ using the std::ofstream class. The file is opened for writing automatically when you use the constructor of std::ofstream with a filename. A new file will be created if the current one does not exist. Its contents will be truncated if the file already exists.