I have a simple question about the constructor of fstream and the .open function. Are there any differences between the following two expression?
fstream("file.txt",ios::app);
fstream fin;
fin.open("file.txt",ios::app);
For (1), I don't need to use .open function again right? Any functional differences between the two expression?
My second question is that if I left the openmode empty, what will be the default open mode?
There are no differences in terms of the state of the objects following your two snippets.
Why are there two versions?
The ctor exists in order to create fstream
objects that are immediately associated with a stream.
The open
exists because these types of objects cannot be copied. Hence you cannot assign an fstream
object to a different stream by writing:
fstream foo('bblskd');
// ...
foo = fstream('skdjf');
(Note that this interface was devised before move semantics).
You can find the default open mode here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With