C++ ofstream Writes formatted output to a file via <<

Description

C++ ofstream Writes formatted output to a file via <<

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()//from   w  w  w.  j  ava  2  s. c o  m
{
   char ch = 'x';
   int j = 77;
   double d = 6.02;
   string str1 = "test";         //strings without
   string str2 = "test test";        //   embedded spaces
   ofstream outfile("fdata.txt"); //create ofstream object
   outfile << ch                  //insert (write) data
           << j
           << ' '                 //needs space between numbers
           << d
           << str1
           << ' '                 //needs spaces between strings
           << str2;
   cout << "File written\n";
   return 0;
}



PreviousNext

Related