#include#include int main() { std::ofstream output("example.txt"); //write data to file here output.close(); //close the file return 0; }
#includeThis example demonstrates how to use the is_open function to check if a file was successfully opened before writing data to it. If the file was successfully opened, the data is written to the file and then the file is closed using the close function. The package library used here is `#include int main() { std::ofstream output("example.txt"); if(output.is_open()) { //write data to file here output.close(); //close the file } else { std::cout << "Error: file could not be opened." << std::endl; } return 0; }