qns on default file modes

from here it says that:
http://www.cplusplus.com/doc/tutorial/files.html
For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the open() member function.

The default value is only applied if the function is called without specifying any value for the mode parameter. If the function is called with any value in that parameter the default mode is overridden, not combined.


these 2 paragraphs seem contradictory to me, 1 says that the ios::in and out are assumed when a mode is passed as a second argument but the other paragraph says that the default value is only applied when no mode argument exists, what do they actually mean?
It means that the implementation of the function could look something like this:
1
2
3
4
/*...*/ open(/*...*/ mode=trunc){ //The default open mode.
	mode|=out;
	//...
}

Is that clear enough?
actually im still not quite clear,
from the 2nd paragraph, this:
ofstream file("file.dat");
would set the ios::out flag

but would this:
ofstream file("file.dat", ios::trunc);
set the ios::out flag as well since it says in the 2nd paragraph that:
default value (in this case, ios::out) is ONLY applied if the function is called WITHOUT specifying any value

but in the first paragraph it says that:
ios::in and ios::out are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the open() member function

Last edited on
The default value is different from the values that are "automatically" assumed. Using helios's example:

1
2
3
4
/* ... */ open(/* ... */ mode = trunc) { // this is the default value; only used if no value is passed
    mode |= out; // out is added to whatever you pass, even if it is the default value
    //...
}
The wording of that paragraph is a bit confusing though.
Last edited on
The problem is actually that that paragraph shouldn't really be there, since it's saying something that doesn't apply specifically to this function, but the whole of C++'s syntax: default parameters only retain their values if the function call doesn't pass them.
Topic archived. No new replies allowed.