Topic: STREAMs vs. STREAMBUFs
Author: Lev REZNIKOV <lreznikv@macs.biu.ac.il>
Date: 1996/08/26 Raw View
Hi all,
I have a question related with Borland C++ classes istream (and derived fstream)
and streambuf (and derived filebuf):
I'm writing some function processing a text, character by character. I have
written two pieces of code:
1) // streams-oriented implementation 2) // streambufs-oriented implementation
ifstream( filename, ios::in ); filebuf inbuf;
// ... inbuf.open( filename, ios::in );
// ...
char sym; char sym;
while( (sym = in.get()) != EOF ) while( (sym = inbuf.sbumpc()) != EOF )
{ {
// ... // ...
} }
Both codes achieve the goal - read file contents character by character, so they
are equivalent considering this aspect. But concerning the efficiency, the
following question arises; If I understand correctly the streambuf class, it
puts apropriate data (in this case, file contents) into the memory and provides
set of functions to handle this memory space. So method sbumpc() explicitly gets
character from the memory. However, what happens to a stream - has it also a
buffer allocated implicitly, and thus method get() also gets its characters from
the memory, or it each time access the storage device for reading?
Obviously, if the latter explanation holds, the efficiency of the first piece of
code would fall far behind the efficiency of the second piece.
On the same subject, is there any way to check whether a stream or a streambuf
is empty?
And what exactly does the manipulator ends with the ostrstrteam obj.? According
to Straustrup, it's supposed to add '\0' and empty the stream - so what 'empty'
means here?
Thanks in advance for help,
Lev Reznikov
P.S. Please, post directly to me, because I access newsgroups on very unregular
base.
Lev REZNIKOV
Computers and Industrial Mathematics Unit (CIMU),
CS and Math. Dept., Bar-Ilan University
Ramat-Gan 52900 ISRAEL
e-mail: lreznikv@macs.biu.ac.il
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1996/08/27 Raw View
Hi,
first a note on crossposting: Please reduce the crossposting to
moderated newsgroups to a necessary minimum (i.e. in normal situation,
there should be only one moderated newsgroup you are posting to)! If
an article is posted to two or more moderated newsgroups, it takes
special action to deal with this article, as the article has to be
approved by a moderator of each of the groups. This process is
somewhat error prone and produces some additional work (actually, there
are moderated newsgroups where crossposting is explicitly disallowed,
to prevent problems with crossposting!). In addition, normally the
articles are not really appropriate for all of the moderated groups.
[Followups set to comp.lang.c++ and comp.lang.c++.moderated]
Lev REZNIKOV (lreznikv@macs.biu.ac.il) wrote:
: If I understand correctly the streambuf class, it
: puts apropriate data (in this case, file contents) into the memory and provides
: set of functions to handle this memory space. So method sbumpc() explicitly gets
: character from the memory. However, what happens to a stream - has it also a
: buffer allocated implicitly, and thus method get() also gets its characters from
: the memory, or it each time access the storage device for reading?
Your understanding is correct but incomplete: the stream classes all
use a 'streambuf' to do the actual streaming: 'iostream::get()' will
call 'streambuf::sbumpc()' (or some other appropriate 'streambuf'
function). What the stream classes add is a convenient way to do
formatted IO. If you don't use formating, you will probably be better
off not to use the stream class but rather use a 'streambuf' directly
or another interface to 'streambuf's which better suits your needs.
: On the same subject, is there any way to check whether a stream or a streambuf
: is empty?
You can use 'streambuf::in_avail()' to figure out how many characters
are at least available. This function can be used for an 'istream',
too, by using the member 'rdbuf()', e.g.
cin.rdbuf()->in_avail()
(this makes explicitly use of the fact, that 'istream' uses a
'streambuf' to manage the actual input). This function is available at
least in a C++ library conforming to the prospective C++ standard. I
don't know whether it is available in the implement you are using.
: And what exactly does the manipulator ends with the ostrstrteam obj.? According
: to Straustrup, it's supposed to add '\0' and empty the stream - so what 'empty'
: means here?
According to the DWP it only puts a string termination character (i.e.
'traits::eos()') at the end of the string. Note, that the use of
'[io]strstream' is deprecated in the DWP: These classes are superseeded
by the classes '[io]stringstream'. These write to/read from a 'string'
instead of using 'char*' this is considered to be less error prone.
: P.S. Please, post directly to me, because I access newsgroups on very unregular
: base.
IMO, a public question deserves a public answer. In addition, it is not
too much work to check a newsgroup for an answer to a specific
question. At least, it is less work than it took to answer the question!
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]