#includeusing namespace std; int main() { ofstream output("example.txt"); if (output.is_open()) { output << "Hello, world!" << endl; output.flush(); output.close(); } return 0; }
#includeIn this example, an ofstream object is created to write data to a file named "example.txt". The numbers 1 through 10 are written to the file, with flush being called on each iteration of the loop to ensure that each number is immediately written to the file. Package/library: The std::ofstream flush function is part of the C++ standard library.#include using namespace std; int main() { ofstream output("example.txt"); if (output.is_open()) { int x = 1; while (x < 11) { output << x << endl; output.flush(); ++x; } output.close(); } return 0; }