Skip to content

Instantly share code, notes, and snippets.

@michaeltrainor
Created February 14, 2018 12:00
Show Gist options
  • Save michaeltrainor/90f5a75e2e80c8038007bb00773f060e to your computer and use it in GitHub Desktop.
Save michaeltrainor/90f5a75e2e80c8038007bb00773f060e to your computer and use it in GitHub Desktop.
Read a Binary File to std::vector<BYTE>
#include <fstream>
#include <iterator>
inline std::vector<uint8_t> read_file_binary(const std::string& file_path)
{
std::ifstream file_buffer(file_path, std::ios::binary);
file_buffer.unsetf(std::ios::skipws);
file_buffer.seekg(0, std::ios::end);
const auto size = file_buffer.tellg();
file_buffer.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer;
buffer.reserve(size);
buffer.insert(buffer.begin(), std::istream_iterator<uint8_t>(file_buffer), std::istream_iterator<uint8_t>());
return buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment