Re: get some chars from a .txt file
Carl Barron wrote:
In article <1169122223.333751.181680@m58g2000cwm.googlegroups.com>,
James Kanze <james.kanze@gmail.com> wrote:
[...]
But this is not a filtering streambuf. It's just a regular
streambuf which writes to a std::vector. (Somewhere in my
code---I don't know if it's in the online stuff or not---I've
got a streambuf based on STL iterators; it's been a while since
I've used it, and I forget the details, but I think it should be
possible to instantiate it for output on a
back_insertion_iterator for the vector.)
It is a filtering streambuf that writes to a vector<char>
If it writes to a vector<char>, it's not a filtering streambuf.
A filtering streambuf writes to or reads from another streambuf.
That's the definition of the term. (And in this case, I think
I'm well placed to speak, since I'm the one who invented the
term.)
After initial thought the possibly best way to
read the rest of the file into a vector was use an operator >>
(streambuf *);
I tend to use this myself, when the target really is a streambuf
(or an ostream). In this case, I certainly wouldn't create a
streambuf just to be able to do it, however; a filtering
streambuf on input and istream_iterator<char> seems a more
appropriate solution.
If the size of the vector can be estimated, reserve()
can improve performance some more.
I guessed this approach should be at least as efficient as using
iterators after noting the istreambuf_iterator<char> solution.
Also not obvious to the 'casual user' of streams...
I suspect that the relative efficiency of all of these
techniques will depend on the actual implementation of the
standard library you are using. With the exception, of course,
of using reserve() if you can estimate a reasonable max. length.
A more general approach if the file is handled a piece at a time is
a 'normal' filtering streambuf to remove '\n' during the input.
Whether the file is handled a piece at a time or not, the
filtering streambuf on input will do the job. (This is one
case, however, where it might be worth using buffering in the
filtering streambuf.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]