Create file



    Giovanny Gongora

    Codiga's C++ Recipes

    Create a text file

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int createFile () {
      // Create a text file
      ofstream MyWriteFile("filename.txt");
      // Write to the file
      MyWriteFile << "Files can be tricky, but it is fun enough!";
      // Close the file
      // Create a text string, which is used to output the text file
      string myText;
      // Read from the text file
      ifstream MyReadFile("filename.txt");
      // Use a while loop together with the getline() function to read the file line by line
      while (getline (MyReadFile, myText)) {
        // Output the text from the file
        cout << myText;
      // Close the file
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.