Re: get some chars from a .txt file
Carl Barron wrote:
In article <1169027836.050953.162390@11g2000cwr.googlegroups.com>,
James Kanze <james.kanze@gmail.com> wrote:
It's also possible to save a copy by reading directly into the
std::vector. The code to do so, however, is considerably more
difficult to get right, and unless your sequences have lengths
measuring in the tens of millions of characters, or more, you
probably won't notice any difference in response time.
A filtering streambuf to remove the end lines can be used
with istream_iterator<char> to fill the vector without the end lines.
That's a possible solution. Since there had been some
discussion concerning performance, and it didn't seem to add
that much, I didn't bother mentionning it, but it is definitly a
possibility. On the other hand...
class nolf_buf:public std::streambuf
{
std::vector<char> &data;
int overflow(int c = EOF)
Overflow is the function called when writing, not when reading.
The function you want to replace is underflow. If you're trying
to use a filtering streambuf on input.
{
try
{
if(c!=EOF && c!='\n')
data.push_back(char(c));
}
catch (...)
{
return EOF;
}
return '0'; // not eof
}
public:
nolf_buf(std::vector<char> &a):data(a){}
};
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.)
,,,
std::vector<char> data;
nolf_buf buf(data);
in_file >> &buf; // data now contains the file contents
....
For example. More idiomatic (but certainly less efficient)
would be to create a real filtering streambuf for the input, and
then do something like:
std::copy( std::istream_iterator< char >( filtered_in_file ),
std::istream_iterator< char >(),
std::back_inserter( data ) ) ;
or simply:
std::vector< char > sequence(
std::istream_iterator< char >( filtered_in_file ),
std::istream_iterator< char >() ) ;
The latter has a certain elegance; the vector never exists
except in its filled state.
--
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! ]