0

Well I'm heavenly confused right now; When would anyone use a streambuffer over a stream - or otherwise?

Reading cppreference and some online topics regarding this only added to the confusion. To me it seems std::basic_istream is an "abstraction" of the buffer. So that one should not have to deal with localization etc.
But you still have to do this when reading file data - so what does it actually abstract away?

On the other hand, what does std::basic_streambuf bring?

And then there's the std::istream_iterator and std::istreambuf_iterator. Which both read elements from the "stream". This adds more confusion: is there any difference in above iterators?

PS: using istream here, but could of course also be ostream or anything else.
PPS: I should add that confusion was added while googling for examples such as this stackoverflow question

Community
  • 1
  • 1
paul23
  • 8,799
  • 12
  • 66
  • 149

1 Answers1

2

std::basic_istream defines user interface: operator>>, read, etc. That's what you call when you want to do input.

std::basic_streambuf defines virtual member functions: underflow, sync, etc. That's what you derive from when you want to write your own input class. boost.iostreams makes it easy.

std::istream_iterator calls operator>> (so it interprets the input as a sequence of objects of some type for which operator>> is defined, goes through locale, skips whitespace, etc)

std::istreambuf_iterator accesses a streambuf directly (so it can only read characters, no locale involved, whitespace isn't special)

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Hmm that makes a little sense, but if what about the special case of reading a file opened through ios_mode::binary then? Also I guess for getting the BOM-mask when reading a file one should then use the buf-iterator? – paul23 Aug 09 '14 at 21:23
  • @paul23 What's so special about it? What does BOM have to do with your question? (in C++11, that's taken care of [by codecvt](http://en.cppreference.com/w/cpp/locale/codecvt_utf16)). Sounds like you have a more specific question in mind. – Cubbi Aug 09 '14 at 21:29
  • Well the exact question I have is found http://stackoverflow.com/questions/4761529/efficient-way-of-reading-a-file-into-an-stdvectorchar there. It uses streambuf iterators, but why doesn't it open the file in binary mode and then read using normal iterators? – paul23 Aug 10 '14 at 10:24