fstream

if instead of ifstream and ofstream we could you use fstream which can do both of the functions why use ifstream and ofstream

Because you might only want to do one or the other.
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
I have no idea what you meant with that last post?
I think he meant that fstream is capable of both operations, so why not just use it?
For the same reason you would declare a variable as an int, and not a double, if it wasn't needed.
Last edited on
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.
Thank you all :)
That makes sense to me too. Thanks Chervil.
Topic archived. No new replies allowed.