I was reading Constructing a vector with istream_iterators which is about reading a complete file contents into a vector of chars. While I want a portion of a file to be loaded in to a vector of chars.
using namespace std;
int main(int argc, char *argv)
std::ifstream ifs(argv, std::ios::binary);
std::vector<char> data(beginItr, endItr);
for_each(data.cbegin(), data.cend(), (char ch)
cout << ch << endl;
cout << distance(beginItr, endItr) << endl;
Why doesn't the advance work on istreambuf_iterator?
It works. It advances the iterator. The problem is that an
istreambuf_iterator is an input iterator but not a forward iterator, meaning it is a single pass iterator: once you advance it, you can never access the previous state again. To do what you want you can simply use an old-fashioned for loop that counts to 4.