Writes formatted output to a file, using << - C++ File Stream

C++ examples for File Stream:stream

Description

Writes formatted output to a file, using <<

Demo Code

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()//from  w w  w. java2 s.c  om
{
   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;
}

Result


Related Tutorials