Writing a File in C++

C++ Program to write text to a file, Here we are using the output stream class ofstream to write the data to the file.

Source code :

#include <iostream>
#include <fstream>

using namespace std;

int main () {
    
  ofstream file;
  string content = "Hello this is CodeCry";
  
  file.open ("sample.txt");
  
  file << content;
  
  file.close();
  
  return 0;
}

Output :

Hello this is CodeCry

Notes :

While compiling this Program, the following text will be written to the file

Hello this is CodeCry

Output :


                

Comments :