write to a file in a class c++ code example Example: write to file in C++ ofstream myfile; myfile.open("file.txt"); myfile << "write this to file"