I prefer std::ifstream and std::ofstream because I don't have to pass flags like std::ios::in and std::ios::out. There is also no chance I accidentally use read operations when using std::ofstream or write operations when using std::ifstream.
Using them, you can refer to a stream that can only be read from or written to, not both. As an example, consider "cin" which can only be read from, not written to. Presumably you might have a file stream like that, and in that case an fstream would be inappropriate.
Why would you close a file you aren't done reading (or writing)?
It is okay to use fstream, but the point is that it is rare to perform both input and output on a file at the same time. It is typically better to stick to reading a file or writing a file at any one time. So, use ifstream or ofstream and get the benefits of code that watches you to not do the wrong thing with your file.
Okay, so to complete, fstream I have freedom to both read and write to the file in the same operation without using the close (), for example if I open a file with fstream used the default constructor and want to read first, then the writing , I will not have to use the close ()?
So this statement and example that I saw on a site is correct?
"The file streams we discussed have a limitation(ifstream and osftream) , they can only do input or output at a time. fstream provides us with a way to read, write randomly without having to close and reopen the file:"
1 2 3 4 5 6 7
//do an input or output here