#includeIn this example, we create an ofstream object to write to a file named "output.txt". We then use the fail function to check if the file was opened successfully. If the file could not be opened, we print an error message to the console and return from the function with an error code. This code example uses the C++ Standard Library.#include int main() { std::ofstream output_file("output.txt"); if (output_file.fail()) { std::cerr << "Failed to open output file." << std::endl; return 1; } output_file << "Hello, world!" << std::endl; output_file.close(); return 0; }