2

What's wrong with this code?

std::vector<unsigned char> newVector;
std::ifstream inFile(fullPath.c_str(), std::ios::in|std::ios::binary);
std::istreambuf_iterator iterator(inFile);

It gives me this:

missing template arguments before 'iterator'

And if I change it to this:

std::istreambuf_iterator<unsigned char> iterator(inFile);

It complains this:

invalid conversion from 'void*' to 
    'std::istreambuf_iterator<unsigned char>::streambuf_type
Mikael S.
  • 1,005
  • 3
  • 14
  • 28

1 Answers1

3

ifstream is a basic_ifstream<char>, not a basic_ifstream<unsigned char>. Therefore, you need to declare iterator as

std::istreambuf_iterator<char> iterator(inFile);

and it will work.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • It doesn't matter if I'm reading a binary file to "unsigned char" vector? – Mikael S. May 06 '14 at 23:06
  • @MikaelS.: No, it'll be fine. For example, you can do `newVector.insert(newVector.begin(), iterator, eos)` where `eos` is a default-initialized (end-of-stream) `istreambuf_iterator`. All that matters there is that the vector element type (`unsigned char`) can be constructed from the iterator element type (`char`). – nneonneo May 06 '14 at 23:11