In: Computer Science
In c++ write a program. Give the necessary statements to open a file and to confirm that the file has been successfully opened for writing.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt"); //ofstream object to write on a
file
if (myfile.is_open())
{
cout<<"File successfully
opened"<<endl;
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Please refer to the screenshot of the code to understand the indentation of the code
Output:
Hope this helped. Please do upvote and if there are any queries please ask in comments section.