C++ binary files and iterators: getting away with a 1:1 using ifstreambuf_iterator? <p><a href="http://stackoverflow.com/a/13665583/2485710">This answer</a> points out the fact that C++ is not well suited for the iteration over a binary file, but this is what I need right now, in short I need to operate on files in a "binary" way, yes all files are binary even the .txt ones, but I'm writing something that operates on image files, so I need to read files that are well structured, were the data is arranged in a specific way.</p> <p>I would like to read the entire file in a data structure such as <code>std::vector&lt;T&gt;</code> so I can almost immediately close the file and work with the content in memory without caring about disk I/O anymore.</p> <p>Right now, the best way to perform a complete iteration over a file according to the standard library is something along the lines of</p> <pre><code>std::ifstream ifs(filename, std::ios::binary); for (std::istreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt; it(ifs.rdbuf()); it != std::istreambuf_iterator&lt;char, std::char_traits&lt;char&gt; &gt;(); it++) { // do something with *it; } ifs.close(); </code></pre> <p>or use <code>std::copy</code>, but even with <code>std::copy</code> you are always using <code>istreambuf</code> iterators ( so if I understand the C++ documentation correctly, you are basically reading 1 byte at each call with the previous code ).</p> <p>So the question is: how do I write a custom iterator ? from where I should inherit from ?</p> <p>I assume that this is also important while writing a file to disk, and I assume that I could use the same iterator class for writing, if I'm wrong please feel free to correct me.</p>