write variable to file cpp

ofstream myfile("example.txt"); if (myfile.is_open()) { myfile << "Writing this to a file.\n"; myfile.close(); } else { cout << "Unable to open file"; }