C++ examples for File Stream:Text File
Write formatted output to a text file.
#include <iostream> #include <fstream> using namespace std; int main()//from www .j a v a 2s . c om { ofstream fout("test.dat"); // Verify that the file has been successfully opened. if(!fout) { cout << "Cannot open file.\n"; return 1; } // Write output to the file. fout << 10 << " " << -20 << " " << 30.2 << "\n"; fout << "This is a test."; // Explicitly close the file. fout.close(); if(!fout.good()) { cout << "A file error occurred."; return 1; } }