Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active April 26, 2016 06:04
Show Gist options
  • Save matthewjberger/0b6760de2ed41436a19235cfbe1b2d31 to your computer and use it in GitHub Desktop.
Save matthewjberger/0b6760de2ed41436a19235cfbe1b2d31 to your computer and use it in GitHub Desktop.
Reads a file into a vector of unsigned char
typedef unsigned char BYTE;
std::vector<BYTE> ReadAllBytes(const std::string& filename)
{
using namespace std;
ifstream file(filename.c_str(), ios::binary | ios::ate);
file >> noskipws;
streampos fileSize = file.tellg();
file.seekg(0, ios::beg);
vector<BYTE> bytes;
bytes.reserve(fileSize);
copy(istream_iterator<BYTE>(file),
istream_iterator<BYTE>(),
back_inserter(bytes));
return bytes;
}
auto bytes = ReadAllBytes("c:/windows/fonts/arialbd.ttf");
unsigned char* data = bytes.data(); // needs one level of indirection to work properly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment