How to Create a File

beginner c++11 files

To open a file for writing in C++ we use std::ofstream (output file stream). Writing is done with the std::ofstream object and the left shift operator(<<), exactly the same as printing.

#include <fstream>

int main() {
  std::ofstream output("example.txt");
  output << "The answer to life, the universe, and everything is ";
  output << 42;
}




For more C++ By Example, click here.