- void open(const char* file_name,ios::openmode mode);
In the above example, new_create1 is an object of type fstream, as we know fstream is a class so we need to create an object of this class to use its member functions. So we create
new_create1 object and call open()function.
Here we use out mode that allows us to open the file to write in it.
Default Open Modes :
- ifstream ios::in
- ofstream ios::out
- fstream ios::in | ios::out
We can combine the different modes using or symbol | .
- new_create1.open("newfilehasopen.txt", ios::out | ios::app );
Here, input mode and append mode are combined which represents the file is opened for writing and appending the outputs at the end.
Example of opening/creating a file using the open() function
using namespace std;
cout<<"File has been failed to create";
cout<<"New file has been created";