If the class ofstream is to write to files, ifstream to read and fstream to both cases, why not always use fstream at once?

Asked

Viewed 36 times

3

Even if we wanted to read, write, modify, create, delete files, or other operations, we could no longer use the class fstream? What problems could they generate if you don’t use those specific?

1 answer

3


fstream serves to read and write to files (can even create if it does not exist), does not serve to delete files.

Under normal conditions, there’s nothing wrong with that. Some people prefer to make clearer what they are doing in each case when they only do one of the operations.

It is even possible to configure the fstream in each moment to only accept reading or only accept writing, so avoid making mistakes, so even in this is equal, as long as you use this. But it is not as semantic as the prepared objects only for each of the operations, and no problems will be detected in the compilation when you try to write in something you just wanted to read and vice versa.

It may be that some specific implementation gives some internal advantage of using one or the other, but then we fall into detail that cannot be answered in general for every language, you have to see the documentation of the implementation to know if there is something different between using one or the other. The contract and how to use it is the same, and even the result, but how to arrive at this result by changing a little. But it would be wiser for it to be the same, I’m just talking about possibility.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.