10

Using std::fstream one can declare objects of both the types ifstream and ofstream. The only difference is that, with fstream we need to provide in, out, app as a parameter which may not always require for other two.

Is there anything special about ifstream,ofstream which cannot be accomplished with fstream or just a coding convenience ?

iammilind
  • 68,093
  • 33
  • 169
  • 336

3 Answers3

16

It's a bit like asking why we'd want const when you can read and write from variables anyway. It allows compile-time checking, an invaluable feature for reducing bugs. It's also more self-documenting, as when looking at a declaration without the constructor call you can see whether it's an input, output or both: the parameters you mention can often only be seen in the implementation file which may not be to hand. Also, each type of stream may have a few differences in the data members they need - potentially using the minimally-functional class matching your actual needs could save memory, time initialising or checking those other variables etc..

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
4

If anything, fstream is the one that's just a convenience. In particular, what you have is basically:

namespace std { 
class ifstream { /* ... */ };

class ofstream { /* ... */ };

class fstream : public ifstream, public ofstream { /* ... */ };
}

[obviously skipping over a lot of irrelevant details].

In short, the fstream provides all of the input capabilities of an ifstream and all the output capabilities of a ofstream by deriving from both ifstream and ofstream. Without ifstream and ofstream, an fstream (at least in anything resembling its current form) couldn't exist at all.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    One irrelevant detail you skipped is it's factually wrong. fstream (basic_fstream actually) does not inherit from either ifstream nor ofstream. – DanielKO Aug 02 '12 at 03:36
  • 3
    @DanielKO: True, but yes, truly irrelevant. If you want to get technical, ifstream derives from istream, and ofstream derives from ostream. fstream derives from iostream, which derives from both istream and ostream (and yes, of course, to be technically correct, we should have various `std::` and `basic_` prefixes and `` suffixes sprinkled around). Do you really think excluding details that even you admit are irrelevant is a sound basis for a downvote? – Jerry Coffin Aug 02 '12 at 03:44
  • But you are implying fstream trivially inherits the implementations of both ifstream and ofstream, as if fstream was just a glue of the other two. You should not oversimplify your answer to the point it does not reflect the facts. I forgot to quote the "irrelevant detail" bit to emphasize the sarcasm, I'm sorry for that. – DanielKO Aug 02 '12 at 03:51
  • 1
    @DanielKO: Can you point to anything relevant that's actually lost in the simplification? The reality is that `fstream` *is* fairly trivial, and yes it is pretty much just glue. The worst you can say is that the glue works in a slightly different "direction", basically gluing an iostream to filestream input and output buffers instead of going to ifstream and ofstream per se. Getting into that lengthens the answer (tremendously) while adding essentially nothing relevant. The `iostream` intermediary is mostly interesting as a base for yet other classes (e.g., `stringstream`). – Jerry Coffin Aug 02 '12 at 04:02
  • 1
    Sure. `void foo(std::ofstream& output); void bar() { std::fstream f("a"); foo(f); }` – DanielKO Aug 03 '12 at 13:47
  • @DanielKO: and how is that relevant to whether `ifstream` and/or `ofstream` provide anything useful and/or should exist? – Jerry Coffin Aug 03 '12 at 14:22
3

The whole point is to be generic. If you only need to read a file, you can take an ifstream as parameter, and then anything which supports reading can be passed in, even if it isn't writeable. And vice versa.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • 2
    I don't see how this answer relates to the question at all. – ildjarn Aug 02 '12 at 03:04
  • @Tony Yeah, I just noticed that ifstream is forced to provide a filebuf object, limiting the potential for alternative implementations. I think the compile time checking and self documentation is a better explanation. – Antimony Aug 02 '12 at 03:05