16

I recall once seeing a clever way of using iterators to read an entire binary file into a vector. It looked something like this:

#include <fstream>
#include <ios>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    ifstream source("myfile.dat", ios::in | ios::binary);
    vector<char> data(istream_iterator(source), ???);
    // do stuff with data
    return 0;
}

The idea is to use vector's iterator range constructor by passing input iterators that specify the entire stream. The problem is I'm not sure what to pass for the end iterator.

How do you create an istream_iterator for the end of a file? Am I completely misremembering this idiom?

2 Answers 2

31

You want the std::istreambuf_iterator<>, for raw input. The std::istream_iterator<> is for formatted input. As for the end of the file, use the stream iterator's default constructor.

std::ifstream source("myfile.dat", std::ios::binary);
std::vector<char> data((std::istreambuf_iterator<char>(source)),
                       std::istreambuf_iterator<char>());

Edited to satisfy C++'s most vexing parse. Thanks, @UncleBens.

4
  • 11
    Beware of the most vexing parse.
    – UncleBens
    Dec 12, 2010 at 19:05
  • Interesting, that this won't compile for me. I have to assign std::istreambuf_iterator<char> to a variable or give it a parameter std::istreambuf_iterator<char>(0). I wonder why. I'm using VS2005.
    – detunized
    Dec 12, 2010 at 19:06
  • 1
    Thanks, that was exactly the pattern I was trying to remember. Dec 13, 2010 at 13:09
  • Finally, I found the answer. Pretty much the only one on SO. Feb 6, 2013 at 10:38
11

In C++11 one could:

std::ifstream source("myfile.dat", std::ios::binary);
std::vector<char> data(std::istreambuf_iterator<char>(source), {});

This shorter form avoids the most vexing parse problem because of the {} argument, which removes ambiguity of it being an argument or a formal parameter.

@wilhelmtell's answer could also be updated to avoid this problem by adopting a brace initializer for data. Still in my view, using {} is more simple and turn the initialization form irrelevant.

EDIT

Or, if we had std::lvalue (and maybe std::xvalue instead of std::move):

#include <vector>
#include <fstream>

template <typename T>
constexpr T &lvalue(T &&r) noexcept { return r; }

int main() {
    using namespace std;

    vector<char> data(
        istreambuf_iterator<char>(lvalue(ifstream("myfile.dat", ios::binary))),
        {}
    );
}
2
  • 1
    Hi @pepper_chico. Am I correct in thinking that when you use {} in the second argument for the data constructor the compiler determines that the type of this argument should be std::istreambuf_iterator<char> and therefore calls its default constructor std::istreambuf_iterator<char>{}? Is that what {} is expanded to?
    – Stan
    Dec 2, 2017 at 14:40
  • 2
    @Stan Yes, it does the same as std::istreambuf_iterator<char>(), it's an useful construction, works like auto, but for values (default values) instead of types alone.
    – oblitum
    Dec 2, 2017 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.