fstream

Dec 20, 2013 at 3:30pm
if instead of ifstream and ofstream we could you use fstream which can do both of the functions why use ifstream and ofstream

Dec 20, 2013 at 3:30pm
Because you might only want to do one or the other.
Dec 20, 2013 at 3:34pm
but w8 cant we write ios::in for ifstream which would imply we are writitng and ios::out for ofstream which would imply we are reading in a sense doesn't that make it simple
Dec 20, 2013 at 3:35pm
I have no idea what you meant with that last post?
Dec 20, 2013 at 5:08pm
I think he meant that fstream is capable of both operations, so why not just use it?
Dec 20, 2013 at 5:49pm
For the same reason you would declare a variable as an int, and not a double, if it wasn't needed.
Last edited on Dec 20, 2013 at 5:49pm
Dec 20, 2013 at 7:30pm
but w8 cant we write ios::in for ifstream which would imply we are writitng and ios::out for ofstream which would imply we are reading in a sense doesn't that make it simple

Perhaps you answered your own question. ios::in implies reading, not writing. And similarly ios::out implies writing, not reading.

That's exactly the opposite of what you wrote. Now that's not a big deal, we all make mistakes. But if we get in the habit of restricting access to only that which is needed, then some other errors can be reduced.

This is similar to the principle of specifying const for functions or parameters where the intention is to not modify the value. Again, this can reduce the possibility of accidentally modifying something by mistake.
Dec 20, 2013 at 10:25pm
Thank you all :)
Dec 21, 2013 at 1:10am
That makes sense to me too. Thanks Chervil.
Topic archived. No new replies allowed.