filebuf vs. fstream

M

Mike Wahler

Maya said:
Apart from being a pointer, what would be the benefit of using
'std::filebuf'

'std::filebuf' is not a pointer. It's a user (the 'user'
here being the standard library) defined type.
than using the std::fstream?

'std::fstream' is a (derived) stream class.
'std::filebuf' is a (derived) stream buffer class.
They're not the same thing. 'filebuf' is the implementation
of an 'fstream's transport layer.
As far as I can see, I would use the same methods in 'filebuf' that I
would when using std::fstream.
No.

Thus my question, what do I gain by using
'std::filebuf' instead of std::fstream?

Confusion. :)

If you want to do file i/o in C++, use the 'fstream',
'ifstream' and/or 'ofstream' classes.

Stream buffer classes can be derived from in order to
create custom i/o handling (often to handle a new type
of device), but this is considered by many to be a more
'advanced' usage of iostreams, not really suitable for
the beginner to work with (but go ahead if you're feeling
adventurous. :) The book I cite below should help greatly.
with that).

A very good explanation of C++ IOStreams, stream buffers,
and how they're designed and work can be found in the
excellent book:
http://www.langer.camelot.de/iostreams.htm

-Mike
 
M

Maya

Apart from being a pointer, what would be the benefit of using
'std::filebuf' than using the std::fstream?
As far as I can see, I would use the same methods in 'filebuf' that I
would when using std::fstream. Thus my question, what do I gain by using
'std::filebuf' instead of std::fstream?
Thanks folks!!
--
E-Comm Canada
~~~~~~~~~~~~~~~~~~~~~~~
Jorge Escalante - Software Development Coordinator.

E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html

* You cannot exercise your power to a point of
humiliation of others.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power. Because they control the minds of the
masses.
- Malcom X
 
D

Dietmar Kuehl

Maya said:
Apart from being a pointer, what would be the benefit of using
'std::filebuf' than using the std::fstream?

1. Where did you get the impression from that 'std::filebuf' is a
pointer?
2. Why do think that using a pointer is a benefit?
3. 'std::filebuf' has an interface suitable to source and sink
characters. 'std::fstream' has an interface to read and write
objects. What is the benefit of using the engine rather than
the whole car? If you are creating your own car, you are better
off encapsulating merely the engine rather than a whole car.
The same applies to 'std::filebuf' with respect to creating a
new mechanism to read or write objects (of course, you would
use 'std::streambuf' in this case for flexibility...).
As far as I can see, I would use the same methods in 'filebuf' that I
would when using std::fstream.

Interesting... I was under the impression that 'std::filebuf' and
'std::fstream' have rather different interfaces and that they are at
different abstraction levels (and, actually different instances of
the proper abstraction 'std::streambuf' and 'std::iostream').
Thus my question, what do I gain by using 'std::filebuf' instead of
std::fstream?

An interface more suitable for binary I/O, for example. 'std::filebuf'
is much more lightweight than 'std::fstream' (it does not carry around
all the formatting stuff), it is faster, and harder to use. Whether
these are advantages you can benefit from depends on what you want to
do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,991
Messages
2,570,214
Members
46,801
Latest member
MandyYtw96

Latest Threads

Top