#include// Create an ofstream object and open the file for writing std::ofstream outfile; outfile.open("example.txt"); // Write data to the file outfile << "Hello, world!" << std::endl; // Close the file outfile.close();
#includeIn this example, we use the `ofstream` class to create an object called `outfile`. We then use the `open` function to open a file called "numbers.txt" for writing. We write a vector of integers to the file using a `for` loop and the `<<` operator. Finally, we close the file using the `close` function. This example also uses the standard library for file input/output.#include int main(){ std::vector numbers = {1, 2, 3, 4, 5}; //Create an ofstream object and open the file for writing std::ofstream outfile; outfile.open("numbers.txt"); //Write data to the file for (auto n : numbers){ outfile << n << std::endl; } //Close the file outfile.close(); return 0; }