C++: How to add raw binary data into source with Visual Studio?

Rookie picture Rookie · Apr 19, 2011 · Viewed 7k times · Source

I have a binary file which i want to embed directly into my source code, so it will be compiled into the .exe file directly, instead of reading it from a file, so the data would already be in the memory when i launch the program.

How do i do this?

Only idea i got was to encode my binary data into base64, put it in a string variable and then decode it back to raw binary data, but this is tricky method which will cause pointless memory allocating. Also, i would like to store the data in the .exe as compact as the original data was.

Edit: The reason i thought of using base64 was because i wanted to make the source code files as small as possible too.

Answer

James Kanze picture James Kanze · Apr 19, 2011

The easiest and most portable way would be to write a small program which converts the data to a C++ source, then compile that and link it into your program. This generated file might look something like:

unsigned char rawData[] =
{
    0x12, 0x34, // ...
};