Often times I find myself needing to read a file in constant memory rather than all at once. Perhaps I need an implementation to scale to arbitrarily large files, or my consumer is already sending in discrete chunks. Either way, I could not find a good public algorithm to do this, and although it is relatively simple, I figured I would share mine. It is internally documented, but feel free to comment with questions (and especially point out any mistakes you find or improvements you can suggest).
I would highly recommend reading Insane Coding’s article, How to read in a file in C++. TLDR: The
read method is the fastest, tied with the C-style
fread using a file pointer (although
istream_iterator is certainly prettiest).
This GitHub Gist is shared under the MIT License, and thus can be used freely, but I would recommend simply understanding the algorithm. The Gist is embedded, if there is no code below, try refreshing.