#include#include using namespace std; int main() { ofstream fout; fout.open("test.txt"); if(fout.is_open()){ cout<<"File opened successfully"<
In this example, we create an object of ofstream named fout and attempt to open a file named "test.txt". The is_open() function is then used to determine whether the file was opened successfully or not.
Example 2:#include#include using namespace std; int main() { ofstream fout("output.txt"); if(fout.is_open()){ fout<<"Hello, World!"< In this example, we create an object of ofstream named fout and open a file named "output.txt". The is_open() function is used to check whether the file was opened successfully or not, and if so, we write a string "Hello, World!" to the file and then close the file. The std::ofstream is_open() function is part of the C++ Standard Library, which is included with the C++ programming language.