Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 220
Rookie Avatar asked Apr 19 '11 14:04

Rookie


1 Answers

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, // ...
};
like image 181
James Kanze Avatar answered Oct 05 '22 09:10

James Kanze