Extracting data from streams
I am currently aghast at the lack of support of reading information from files in C++.
I can't find a good way to read a chunk of data from them.
My current goals are to be able to iterate over the contents of a file using random access/forward iterators (because I need ++, += and ), which, of course, stream iterators doesn't support (pathetic things).
So I thought I'd implement a buffered stream iterator that reads chunks of data from the file and allows for random access to it.
What I'm stuck on, however, is reading that chunks of data from the file.
I have two approaches:
(IndiceType is a typedef for std::size_t.)
This approach uses a vector and indices to manually resize the vector and read a chunk of data from the file. Unfortunately, I don't know how much data it reads.
IndiceType BufferSize = m_Buffer.size();
m_Buffer.resize(BufferSize == 0 ? 1 * MB : BufferSize * 2);
m_ItEnd = m_Buffer.size();
m_pFile->read((&*m_Buffer.begin() + m_ItCurrent),
std::streamsize(m_ItEnd - m_ItCurrent));
(T is a template type for char or wchar_t.)
(BufType is a typedef for std::vector<T>.)
Another approach is this. Basically std::copy with a istream iterator and a back_insert iterator. But it's painfully slow, so it's out of the question, even though it works.
std::istream_iterator<T, T> SrcIt(*m_pFile);
std::istream_iterator<T, T> EndIt;
int i = 0;
for (;; i++)
*DstIt++ = *SrcIt++;
if (SrcIt == EndIt || i == 1 * MB)
m_ItEnd += i;
Another thing that bugs me is that I can't use std::copy, because I can't get an iterator that is 1 MB bytes away from the begin iterator, since istream_iterator doesn't support operator +.
So any good ideas on how to do this? C++ is worse in this area than C is, really.
It's truly a shame.
Member variables in the class that code uses:
bool m_End; // Is *this == end()?