Mega Code Archive

 
Categories / C++ Tutorial / File Stream
 

Write formatted output to a text file

#include <iostream> #include <fstream> using namespace std; int main() {   ofstream fout("test.dat");   if(!fout) {     cout << "Cannot open file.\n";     return 1;   }   fout << 10 << " " << -20 << " " << 30.2 << "\n";   fout << "This is a test.";   fout.close();   if(!fout.good()) {     cout << "A file error occurred.";     return 1;   } }