A few weeks back I was looking at the choice of whether to use iterators or streaming operations for I/O on my data compression code. I was bemoaning the fact that the C++ iterators that perform stream I/O use the insertion and extraction operators, making them unsuitable for binary data compression.
It looks like I spoke too fast.
The canonical way to use iterators to go over an iostream is to use
or its partner,
ostream_iterator. In this short sample, I create a binary file
with values 0 to 256, then try to read back the data using this iterator type:
Running this program gives the following output:
Error on character number: 8 Error on character number: 31 Count: 250
Due to the fact that the extraction operator uses whitespace as a delimiter, we get errors trying to read tab, line feed, vertical tab, form feed, carriage return, and space - which means we simply don’t see six characters in the input file. This is not too bad when parsing ASCII text, but when trying to do compression on binary data, it just won’t work.
I figured this was the end of it until reader Fred Jardon chimed in with:
istreambuf_iteratorthe iterator you're looking for? See https://en.cppreference.com/w/cpp/iterator/istreambuf_iterator It directly reads from the inner streambuf without using the extraction operator. The opposite iterator exists:
Well, Fred is quite right, and it only serves to show my lack of depth when it comes to
iostreams. The two classes,
read directly from the underlying buffer, and don’t use the extraction operator, which means
they don’t have the whitespace issues seen above. Changing just two lines fixes the problem:
Running that I get the pristine output I long for:
I’d like to tell you the ramifications of diving down into the object and working on the buffer,
but I’m afraid this implementation details still elude me. There’s a lot more going on
in iostreams than in good old
<stdio.h>, and half of what I learned about it was
wiped out in the standardization process. So be it.
In any case, I think the proper use of
istreambuf_iterator<char> tips the scale
in favor of iterators for me, which reverses my previous thinking.
And many thanks to Fred for straightening out the mess.