confusion on ifstream and ofstream

Dec 8, 2009 at 12:25am
I was reading the tutorial on Input/Output with files and I came across two sentences that seemed (to me) to contradict each other.


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.


To me the first paragraph sounds like it is saying that ios::in and ios::out are true whether the second parameter (the mode) for file open is specified or not. For example:

1
2
ofstream myfile;
myfile.open ("example.bin", ios::binary); 


The first paragraph seems to be saying that ios::in would be true for myfile as well.

However the second paragraph seems to be saying the complete opposite. To me it is saying ios::in and ios::binary are not combined and thus the default value ios::in is overridden. Can someone please help me with this confusion?
Dec 8, 2009 at 12:59am
You have a good point.

Have you tried testing that and seeing if it works? IMO the ios::in/out should be there always, even if the default value is overridden.
Dec 8, 2009 at 1:17am
That's what I think as well. No I haven't tested it, (I have no need to read a binary file) but I thought I'd ask for the sake of understanding.

Dec 8, 2009 at 1:30am
I was just looking at this in the Boost Iostream library and there, the first statement is what is implemented. std::ios::in is always appended to the mode flag for an ifstream. Same with ios::out and ofstream. I would assume that is what is specified in the standard.
Topic archived. No new replies allowed.