Writing to a File using Ofstream class





This video tutorial explains how to write to a file using an ofstream class object.
You are gonna learn how to create an object of ofstream class, how to open a file for writing, how to associate a file with an object of ofstream class, how to write to the file using stream insertion operator in detail with example.

source code for this tutorial

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ofstream file("anil.txt");

  if(!file.is_open()){
    cout <<"unable to open the file"<<endl;

  }else{

  file << "hi am anil shetty"<<endl;
  file << "am wesome...";
  file.close();
  cout << "successfully written to the file open it and check it out";

  }

       return 0;
}

 
Back To Top