C++ File Handling:: open()

C++ File Handling:: open()

                                        Opening a File

The first operation performed on an object of one of these classes is to associate it to a real file. This procedure is known to open a file.

  We can open a file using any one of the following methods:

    1. First is bypassing the file name in constructor at the time of object creation.
    2. Second is using the open() function.

To open a file use

  • open() function

  • void open(const char* file_name,ios::openmode mode);

fstream new_create1;
new_create1.open("newfilehasopen.txt", ios::out);

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 | .


     ofstream new_file;

  • 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

  #include <fstream>
   using namespace std;
  int main()
   fstream new_create1; 
  cout<<"File  has been failed to create";
    cout<<"New file has been created";
  return 0;


Name Views Likes
C++ Exception Handling-|| 85 3
C++ Exception Handling 116 3
C++ File Handling EOF 94 5
C++ File Handling Error Handling During file Operation 1135 5
C++ File Handling seekp() Function 106 5
C++ File Handling seekg() Function 105 6
C++ File Handling tellg() Function 127 5
C++ File Handling tellp() Function 133 5
C++ File Handling close() Function 78 5
C++ File Handling:: open() 107 5
C++ File Handling Introduction 131 5
C++ Container Library:: std ::array-II 102 4
Top Django Interview Questions and Answers You Need to Know 303 6
C++ Container Library::std::array 97 6
C++ Container Library Introduction 104 5
Essential Engineering Skills for Your Resume 127 5
Mistakes To Avoid During Technical Interview 133 5
Mistakes You Must Avoid During Work From Home 179 6
Seven Common Mistakes that Beginners Should Avoid While Learning to Code 109 6
Simple Linear Regression 96 6
Python wagtailmenus 123 6
Python wagtail application 120 6
Python guppy GSL 136 5
Python wagtail guppy subpackage heapy 174 6
Python guppy Introduction 215 6
Python wagtail Structural Block 258 6
Python wagtail Integrating into Django Project 370 6
Python wagtail testing 281 7
Python wagtail Basic Block 349 7
Python wagtail image tag 216 7
Python wagtail Templates 180 6
Python wagtail Backends 206 6
Python wagtail Indexing 190 6
Python wagtail Search 173 7
Python wagtail Snippets 192 6
Python wagtail Writing your own page models 165 9
Python wagtail model class PageRevision 196 10
Python wagtail model class Site 199 9
Python wagtail wagtail core model 219 9
Python wagtail page models 189 10
Python wagtail 166 11