Computer Science, asked by Pandusmilie2353, 1 year ago

What is the difference between opening a file with constructor and opening a file with open() function.

Answers

Answered by prakhargupta3301
5

Explanation:

Not much difference.

Examples:

Constructor:

ofstream o("test.dat",ios::app,ios::binary);

Open function:

ofstream o;

o.open();

The difference is that if you already have made the file, it makes no sense to type the same argument in a constructor and pass it to the class (class of ofstream).

So, you can use o.open() function.

However if you want to create an object of ofstream, you need to use constructor method the first time.

In the same block i.e. { }, if you have closed the file using o.close(), you can open the same file again by simply calling o.open().

                 

Similar questions